Index: src/OFFloatMatrix.h ================================================================== --- src/OFFloatMatrix.h +++ src/OFFloatMatrix.h @@ -16,10 +16,12 @@ #include #import "OFObject.h" +@class OFFloatVector; + /** * \brief A class for storing and manipulating matrices of floats. */ @interface OFFloatMatrix: OFObject { @@ -168,6 +170,20 @@ /** * \brief Transposes the receiver. */ - (void)transpose; + +/** + * \brief Translates the nxn matrix of the receiver with an n-1 vector. + * + * \param vector The vector to translate with + */ +- (void)translateWithVector: (OFFloatVector*)vector; + +/** + * \brief Scales the nxn matrix of the receiver with an n-1 vector. + * + * \param scale The vector to scale with + */ +- (void)scaleWithVector: (OFFloatVector*)vector; @end Index: src/OFFloatMatrix.m ================================================================== --- src/OFFloatMatrix.m +++ src/OFFloatMatrix.m @@ -18,10 +18,11 @@ #include #include #import "OFFloatMatrix.h" +#import "OFFloatVector.h" #import "OFString.h" #import "OFInvalidArgumentException.h" #import "OFNotImplementedException.h" #import "OFOutOfRangeException.h" @@ -367,6 +368,53 @@ } [self freeMemory: data]; data = newData; } + +- (void)translateWithVector: (OFFloatVector*)vector +{ + OFFloatMatrix *translation; + float *cArray; + + if (rows != columns || [vector dimension] != rows - 1) + @throw [OFInvalidArgumentException newWithClass: isa + selector: _cmd]; + + cArray = [vector cArray]; + translation = [[OFFloatMatrix alloc] initWithRows: rows + columns: columns]; + + memcpy(translation->data + (columns - 1) * rows, cArray, + (rows - 1) * sizeof(float)); + + @try { + [self multiplyWithMatrix: translation]; + } @finally { + [translation release]; + } +} + +- (void)scaleWithVector: (OFFloatVector*)vector +{ + OFFloatMatrix *scale; + float *cArray; + size_t i, j; + + if (rows != columns || [vector dimension] != rows - 1) + @throw [OFInvalidArgumentException newWithClass: isa + selector: _cmd]; + + cArray = [vector cArray]; + scale = [[OFFloatMatrix alloc] initWithRows: rows + columns: columns]; + + for (i = j = 0; i < ((rows - 1) * columns) - 1; i += rows + 1) + scale->data[i] = cArray[j++]; + + @try { + [self multiplyWithMatrix: scale]; + } @finally { + [scale release]; + } +} @end