• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: A Matrix collection class
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A Matrix collection class


  • Subject: Re: A Matrix collection class
  • From: Timothy Ritchey <email@hidden>
  • Date: Wed, 8 May 2002 08:13:36 -0500

here is a VERY simple matrix class that I had squirreled away. Somewhere, I even have an altivec enhanced version I was working on:

//
// RRMatrix.h
// RRMatrix
//
// Created by Timothy Ritchey on Fri Mar 08 2002.
// Copyright (c) 2002 Timothy Ritchey. All rights reserved.
//

#import <Foundation/Foundation.h>

// this is the basic brain-dead dense matrix implementation
@interface RRMatrix : NSObject {
unsigned int m;
unsigned int n;
float **data;
}
- (id)initWithRows:(unsigned int)rowCount columns:(unsigned int)columnCount;

- (unsigned int)rows;
- (unsigned int)columns;
- (float**)data;

- (float)row:(unsigned int)row column:(unsigned int)column;
- (void)setRow:(unsigned int)row column:(unsigned int)column toValue:(float)value;

// basic functionality
- (RRMatrix*)add:(RRMatrix*)rhs;
- (RRMatrix*)subtract:(RRMatrix*)rhs;
- (RRMatrix*)multiply:(RRMatrix*)rhs;
- (RRMatrix*)transpose;

// in-place versions of add/subtract
- (void)add:(RRMatrix*)rhs in:(RRMatrix*)place;
- (void)subtract:(RRMatrix*)rhs in:(RRMatrix*)place;
- (void)multiply:(RRMatrix*)rhs in:(RRMatrix*)place;

- (void)transposeIn:(RRMatrix*)place;


@end


//
// RRMatrix.m
// RRMatrix
//
// Created by Timothy Ritchey on Fri Mar 08 2002.
// Copyright (c) 2002 Timothy Ritchey. All rights reserved.
//

#import "RRMatrix.h"

@implementation RRMatrix

- (id)initWithRows:(unsigned int)rowCount columns:(unsigned int)columnCount
{
int i;
float *array = malloc(sizeof(float)*rowCount*columnCount);
if(self = [super init]) {
m = rowCount;
n = columnCount;
data = (float**)malloc(sizeof(float*)*m);
for(i = 0; i < m; i++)
data[i] = &(array[i*n]);
}
return self;
}

- (id)copyWithZone:(NSZone*)zone
{
RRMatrix *copy = [[RRMatrix allocWithZone:zone] initWithRows:m columns:n];
float **copy_data = [copy data];
int i, j;
for(i = 0; i < m; i++) {
for(j = 0; j < n; ++j) {
copy_data[i][j] = data[i][j];
}
}
return copy;
}

- (unsigned int)rows
{
return m;
}

- (unsigned int)columns
{
return n;
}

- (float**)data
{
return data;
}

- (float)row:(unsigned int)row column:(unsigned int)column
{
// note: this is column major ordering
return data[row][column];
}

- (void)setRow:(unsigned int)row column:(unsigned int)column toValue:(float)value
{
data[row][column] = value;
}


// basic functionality
- (RRMatrix*)add:(RRMatrix*)rhs
{
RRMatrix *newMatrix = [self copy];
[newMatrix add:rhs in:newMatrix];
[newMatrix autorelease];
return newMatrix;
}

- (RRMatrix*)subtract:(RRMatrix*)rhs
{
RRMatrix *newMatrix = [self copy];
[newMatrix subtract:rhs in:newMatrix];
[newMatrix autorelease];
return newMatrix;
}

- (RRMatrix*)multiply:(RRMatrix*)rhs
{
RRMatrix *newMatrix = [[RRMatrix alloc] initWithRows:m columns:n];
[self multiply:rhs in:newMatrix];
[newMatrix autorelease];
return newMatrix;
}

- (RRMatrix*)transpose
{
RRMatrix *newMatrix = [[RRMatrix alloc] initWithRows:n columns:m];
[self transposeIn:newMatrix];
[newMatrix autorelease];
return newMatrix;
}

// in-place versions of add/subtract/multiply
- (void)add:(RRMatrix*)rhs in:(RRMatrix*)place
{
NSException *matrixException;
unsigned int i, j;
float **rhs_data = [rhs data];
float **place_data = [place data];

// the matrices must be the same size
if(m != [rhs rows] || n != [rhs columns]) {
matrixException = [NSException
exceptionWithName:@"UnequalMatrices"
reason:@"Attempt to Add Unequal Matrices"
userInfo:nil];
[matrixException raise];
}

for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
place_data[i][j] = data[i][j] + rhs_data[i][j];
}
}

}

- (void)subtract:(RRMatrix*)rhs in:(RRMatrix*)place
{
NSException *matrixException;
unsigned int i, j;
float **rhs_data = [rhs data];
float **place_data = [place data];

// the matrices must be the same size
if(m != [rhs rows] || n != [rhs columns]) {
matrixException = [NSException
exceptionWithName:@"UnequalMatrices"
reason:@"Attempt to Subtract Unequal Matrices"
userInfo:nil];
[matrixException raise];
}

for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
place_data[i][j] = data[i][j] - rhs_data[i][j];
}
}
}

- (void)multiply:(RRMatrix*)rhs in:(RRMatrix*)place
{
int i, j, k, l = [rhs columns];
float **c = [place data];
float **a = [self data];
float **b = [rhs data];

for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
c[i][j] = 0;
for(k = 0; k < l; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
}

- (void)transposeIn:(RRMatrix*)place
{

}


- (void)dealloc
{
free(data[0]);
free(data);
[super dealloc];
}

@end

On Wednesday, May 8, 2002, at 07:46 AM, email@hidden wrote:

My main problem with multidimensional arrays is figuring out how to convert a straight void* to them - I can never persuade C to convert to int[x][y][]... And while I know I can use my own index calculation, or an NSDictionary, I was hoping (as I said) for something less ugly. An object that does that for me, for example.

Kritter out

The problem with that approach is that it gets very memory and time
inefficient if you have several dimensions - need to set up an awful lot
of NSArrays. I was hoping for something a little less ugly.

Depends on how filled your array is. If it is well filled, it's not that
bad (compared with the actual array contents), but of course, always you
can consider using one big array and computing the index yourself
(i*SIZEI+ j*SIZEJ+k) or even using directly plain C multidimensional
array, which does exactly the same.

OTOH, if the array happens to be sparse, I guess NSDictionary is the best
approach. ---
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: A Matrix collection class (From: email@hidden)

  • Prev by Date: Re: A Matrix collection class
  • Next by Date: Re: A Matrix collection class
  • Previous by thread: Re: A Matrix collection class
  • Next by thread: Re: A Matrix collection class
  • Index(es):
    • Date
    • Thread