Index: src/OFFloatVector.h ================================================================== --- src/OFFloatVector.h +++ src/OFFloatVector.h @@ -16,10 +16,12 @@ #include #import "OFObject.h" +@class OFFloatMatrix; + /** * \brief A class for storing and manipulating vectors of floats. */ @interface OFFloatVector: OFObject { @@ -162,6 +164,14 @@ /** * \brief Normalizes the vector. */ - (void)normalize; + +/** + * \brief Multiplies the receiver with the specified matrix on the left side and + * the receiver on the right side. + * + * \param matrix The matrix to multiply the receiver with + */ +- (void)multiplyWithMatrix: (OFFloatMatrix*)matrix; @end Index: src/OFFloatVector.m ================================================================== --- src/OFFloatVector.m +++ src/OFFloatVector.m @@ -18,10 +18,11 @@ #include #include #import "OFFloatVector.h" +#import "OFFloatMatrix.h" #import "OFString.h" #import "OFInvalidArgumentException.h" #import "OFNotImplementedException.h" #import "OFOutOfRangeException.h" @@ -315,6 +316,40 @@ magnitude = [self magnitude]; for (i = 0; i < dimension; i++) data[i] /= magnitude; } + +- (void)multiplyWithMatrix: (OFFloatMatrix*)matrix +{ + size_t rows, columns; + float *cArray, *newData; + size_t i, j, k; + + columns = [matrix columns]; + + if (dimension != columns) + @throw [OFInvalidArgumentException newWithClass: isa + selector: _cmd]; + + rows = [matrix rows]; + cArray = [matrix cArray]; + + newData = [self allocMemoryForNItems: rows + withSize: sizeof(float)]; + memset(newData, 0, rows * sizeof(float)); + + for (i = j = k = 0; i < rows * columns; i++) { + newData[j] += cArray[i] * data[k]; + + if (++j == rows) { + k++; + j = 0; + } + } + + [self freeMemory: data]; + data = newData; + + dimension = rows; +} @end