Skip to content

Commit

Permalink
Merge pull request #3 from samkrishna/master
Browse files Browse the repository at this point in the history
Merger
  • Loading branch information
KingIsulgard authored Jun 19, 2017
2 parents 4e97700 + ed90944 commit de3bbf9
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 226 deletions.
6 changes: 3 additions & 3 deletions PolynomialRegression.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
CA11100C1ABB16560007758E /* DoublesMatrix.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DoublesMatrix.h; sourceTree = "<group>"; };
CA11100D1ABB16560007758E /* DoublesMatrix.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DoublesMatrix.m; sourceTree = "<group>"; };
CA547DFF1ABF319800E10ECC /* PolynomialRegression.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PolynomialRegression.m; sourceTree = "<group>"; };
CA11100C1ABB16560007758E /* DoublesMatrix.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = DoublesMatrix.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
CA11100D1ABB16560007758E /* DoublesMatrix.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = DoublesMatrix.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
CA547DFF1ABF319800E10ECC /* PolynomialRegression.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = PolynomialRegression.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
CA547E011ABF31A700E10ECC /* PolynomialRegression.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PolynomialRegression.h; sourceTree = "<group>"; };
CA80200A1AB9B747007CDF96 /* PolynomialRegression.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PolynomialRegression.app; sourceTree = BUILT_PRODUCTS_DIR; };
CA80200E1AB9B747007CDF96 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down
35 changes: 16 additions & 19 deletions PolynomialRegression/DoublesMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,28 @@

#import <Foundation/Foundation.h>

@interface DoublesMatrix : NSObject {
NSMutableArray *values;
int rows;
int columns;
}
@interface DoublesMatrix : NSObject

@property (nonatomic) int rows;
@property (nonatomic) int columns;
@property (nonatomic, readwrite, strong) NSMutableArray *values;
@property (nonatomic, readwrite, assign) NSUInteger rows;
@property (nonatomic, readwrite, assign) NSUInteger columns;

- (id) initWithSizeRows: (int) m columns: (int) n;
- (void) expandToRows: (int) m columns: (int) n;
- (void) setValueAtRow: (int) m column: (int) n value: (double) value;
- (double) getValueAtRow: (int) m column: (int) n;
- (instancetype)initWithSizeRows:(NSUInteger)m columns:(NSUInteger)n;
- (void)expandToRows:(NSUInteger)m columns:(NSUInteger)n;
- (void)setValueAtRow:(NSUInteger)m column:(NSUInteger)n value:(double)value;
- (double)valueAtRow:(NSUInteger)m column:(NSUInteger)n;

- (DoublesMatrix *) transpose;
- (DoublesMatrix *) multiplyWithMatrix: (DoublesMatrix *) matrix;
- (DoublesMatrix *)transpose;
- (DoublesMatrix *)multiplyWithMatrix:(DoublesMatrix *)matrix;

- (void) rotateLeft;
- (void) rotateRight;
- (void)rotateLeft;
- (void)rotateRight;

- (void) rotateTop;
- (void) rotateBottom;
- (void)rotateTop;
- (void)rotateBottom;

- (double) determinant;
- (double)determinant;

- (DoublesMatrix *) duplicate;
- (DoublesMatrix *)duplicate;

@end
179 changes: 90 additions & 89 deletions PolynomialRegression/DoublesMatrix.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,29 @@

@implementation DoublesMatrix

@synthesize rows;
@synthesize columns;

/**
* Matrix init
*
* Create an empty matrix with zeros of size rowsxcolumns
*/
- (id) initWithSizeRows: (int) m columns: (int) n
- (instancetype)initWithSizeRows:(NSUInteger)m columns:(NSUInteger)n
{
if ( self = [super init] )
{
rows = m;
columns = n;
values = [[NSMutableArray alloc] init];
if (!(self = [super init])) return nil;

_rows = m;
_columns = n;
_values = [[NSMutableArray alloc] init];

for (int i = 0; i < m; i++) {
NSMutableArray *nValues = [[NSMutableArray alloc] init];

for(int i = 0; i < m; i++) {
NSMutableArray *nValues = [[NSMutableArray alloc] init];

for(int j = 0; j < n; j++) {
[nValues addObject: [NSNumber numberWithDouble: 0]];
}

[values addObject: nValues];
for (int j = 0; j < n; j++) {
[nValues addObject: @0];
}

[_values addObject: nValues];
}

return self;
}

Expand All @@ -46,29 +42,31 @@ - (id) initWithSizeRows: (int) m columns: (int) n
*
* Resize the array to a bigger size if needed
*/
- (void) expandToRows: (int) m columns: (int) n
- (void)expandToRows:(NSUInteger)m columns:(NSUInteger)n
{
if(columns < n) {
for(int i = 0; i < rows; i++) {
int adder = n - columns;
for(int j = 0; j < adder; j++) {
[[values objectAtIndex: i] addObject: [NSNumber numberWithDouble: 0]];
if (self.columns < n) {
for (int i = 0; i < self.rows; i++) {
NSUInteger adder = n - self.columns;
for (int j = 0; j < adder; j++) {
[self.values[i] addObject: @0];
}
}
columns = n;

self.columns = n;
}

if(rows < m) {
int adder = m - rows;
for(int i = 0; i < adder; i++) {
if (self.rows < m) {
NSUInteger adder = m - self.rows;
for (int i = 0; i < adder; i++) {
NSMutableArray *nValues = [[NSMutableArray alloc] init];
for(int j = 0; j < n; j++) {
[nValues addObject: [NSNumber numberWithDouble: 0]];
for (int j = 0; j < n; j++) {
[nValues addObject: @0];
}
[values addObject: nValues];

[self.values addObject: nValues];
}
rows = m;

self.rows = m;
}
}

Expand All @@ -77,28 +75,28 @@ - (void) expandToRows: (int) m columns: (int) n
*
* Set the value at a certain row and column
*/
- (void) setValueAtRow: (int) m column: (int) n value: (double) value
- (void)setValueAtRow:(NSUInteger)m column:(NSUInteger)n value:(double)value
{
if(m >= rows || n >= columns) {
if (m >= self.rows || n >= self.columns) {
[self expandToRows: (m + 1) columns: (n + 1)];
}

NSNumber *val = [NSNumber numberWithDouble: value];
[[values objectAtIndex: m] setObject: val atIndex: n];
NSNumber *val = @(value);
[self.values[m] setObject: val atIndex: n];
}

/**
* Matrix getvalue
*
* Get the value at a certain row and column
*/
- (double) getValueAtRow: (int) m column: (int) n
- (double)valueAtRow:(NSUInteger)m column:(NSUInteger)n
{
if(m >= rows || n >= columns) {
if (m >= self.rows || n >= self.columns) {
[self expandToRows: (m + 1) columns: (n + 1)];
}

return [[[values objectAtIndex: m] objectAtIndex: n] doubleValue];
return [self.values[m][n] doubleValue];
}

/**
Expand All @@ -115,12 +113,13 @@ - (double) getValueAtRow: (int) m column: (int) n
*
* @link http://en.wikipedia.org/wiki/Transpose Wikipedia
*/
- (DoublesMatrix *) transpose {
DoublesMatrix *transposed = [[DoublesMatrix alloc] initWithSizeRows: self.columns columns: self.rows];
- (DoublesMatrix *)transpose
{
DoublesMatrix *transposed = [[DoublesMatrix alloc] initWithSizeRows:self.columns columns:self.rows];

for(int i = 0; i < self.rows; i++) {
for(int j = 0; j < self.columns; j++) {
double value = [self getValueAtRow: i column: j];
for (int i = 0; i < self.rows; i++) {
for (int j = 0; j < self.columns; j++) {
double value = [self valueAtRow: i column: j];
[transposed setValueAtRow: j column: i value: value];
}

Expand All @@ -140,22 +139,19 @@ - (DoublesMatrix *) transpose {
*
* @link http://en.wikipedia.org/wiki/Matrix_multiplication Wikipedia
*/
- (DoublesMatrix *) multiplyWithMatrix: (DoublesMatrix *) matrix {
if(self.columns != matrix.rows) {
[NSException raise:@"There should be as many columns in matrix A (this matrix) as there are rows in matrix B (parameter matrix) to multiply. " format: @"Matrix A has %d columns and matrix B has %d rows.", self.columns, matrix.rows];
return nil;
}
- (DoublesMatrix *) multiplyWithMatrix: (DoublesMatrix *) matrix
{
NSCAssert(self.columns == matrix.rows, @"There should be as many columns in matrix A (this matrix) as there are rows in matrix B (parameter matrix) to multiply. Matrix A has %lu columns and matrix B has %lu rows.", self.columns, matrix.rows);

// The result of a mxn matrix multiplied with an nxp matrix resulsts in a mxp matrix
DoublesMatrix *result = [[DoublesMatrix alloc] initWithSizeRows: rows columns: matrix.columns];
DoublesMatrix *result = [[DoublesMatrix alloc] initWithSizeRows:self.rows columns:matrix.columns];

for(int r_col = 0; r_col < matrix.columns; r_col++) {

for(int l_row = 0; l_row < rows; l_row++) {
for (int r_col = 0; r_col < matrix.columns; r_col++) {
for (int l_row = 0; l_row < self.rows; l_row++) {
// For field Rij we need to make the sum of AixBxj
double value = 0.0f;
for(int col = 0; col < columns; col++) {
value += ([self getValueAtRow: l_row column: col] * [matrix getValueAtRow: col column: r_col]);
for (int col = 0; col < self.columns; col++) {
value += ([self valueAtRow: l_row column: col] * [matrix valueAtRow: col column: r_col]);
}
[result setValueAtRow: l_row column: r_col value: value];
}
Expand All @@ -169,11 +165,12 @@ - (DoublesMatrix *) multiplyWithMatrix: (DoublesMatrix *) matrix {
*
* Rotate all row elements in the matrix one column to the left
*/
- (void) rotateLeft {
- (void)rotateLeft
{
// Shift all rows
for(int m = 0; m < rows; m++) {
NSMutableArray *row = [values objectAtIndex: m];
NSNumber *shiftObject = [row objectAtIndex: 0];
for (int m = 0; m < self.rows; m++) {
NSMutableArray *row = self.values[m];
NSNumber *shiftObject = row[0];
[row removeObjectAtIndex: 0];
[row addObject: shiftObject];
}
Expand All @@ -184,12 +181,13 @@ - (void) rotateLeft {
*
* Rotate all row elements in the matrix one column to the right
*/
- (void) rotateRight {
- (void)rotateRight
{
// Shift all rows
for(int m = 0; m < rows; m++) {
NSMutableArray *row = [values objectAtIndex: m];
NSNumber *shiftObject = [row objectAtIndex: columns - 1];
[row removeObjectAtIndex: columns - 1];
for (int m = 0; m < self.rows; m++) {
NSMutableArray *row = self.values[m];
NSNumber *shiftObject = row[self.columns - 1];
[row removeObjectAtIndex: self.columns - 1];
[row insertObject: shiftObject atIndex: 0];
}
}
Expand All @@ -199,21 +197,23 @@ - (void) rotateRight {
*
* Rotate all column elements in the matrix one row to the top
*/
- (void) rotateTop {
NSMutableArray *row = [values objectAtIndex: 0];
[values removeObjectAtIndex: 0];
[values addObject: row];
- (void)rotateTop
{
NSMutableArray *row = self.values[0];
[self.values removeObjectAtIndex: 0];
[self.values addObject: row];
}

/**
* Matrix rotateBottom
*
* Rotate all column elements in the matrix one row to the bottom
*/
- (void) rotateBottom {
NSMutableArray *row = [values objectAtIndex: rows - 1];
[values removeObjectAtIndex: rows - 1];
[values insertObject: row atIndex: 0];
- (void)rotateBottom
{
NSMutableArray *row = self.values[self.rows - 1];
[self.values removeObjectAtIndex: self.rows - 1];
[self.values insertObject: row atIndex: 0];
}

/**
Expand All @@ -230,28 +230,27 @@ - (void) rotateBottom {
*
* @link http://en.wikipedia.org/wiki/Determinant Wikipedia
*/
- (double) determinant {
- (double)determinant
{
double det = 0;

for(int i = 0; i < rows; i++) {
for (int i = 0; i < self.rows; i++) {
double product = 1;

for(int j = 0; j < columns; j++) {
int column = (int) fmodf(i + j, columns);
int row = (int) fmodf(j, rows);

product *= [self getValueAtRow: row column: column];
for (int j = 0; j < self.columns; j++) {
NSUInteger column = (NSUInteger) fmodf(i + j, self.columns);
NSUInteger row = (NSUInteger) fmodf(j, self.rows);
product *= [self valueAtRow: row column: column];
}

det += product;

product = 1;

for(int j = 0; j < columns; j++) {
int column = (int) fmodf(i - j + columns, columns);
int row = (int) fmodf(j, rows);

product *= [self getValueAtRow: row column: column];
for (int j = 0; j < self.columns; j++) {
NSUInteger column = (NSUInteger)fmodf(i - j + self.columns, self.columns);
NSUInteger row = (NSUInteger)fmodf(j, self.rows);
product *= [self valueAtRow: row column: column];
}

det -= product;
Expand All @@ -265,15 +264,17 @@ - (double) determinant {
*
* Creates a duplicate TwoDimensionalMatrixOfDoubles of the current matrix
*/
- (DoublesMatrix *) duplicate {
DoublesMatrix *duplicate = [[DoublesMatrix alloc] initWithSizeRows: rows columns: columns];
- (DoublesMatrix *)duplicate
{
DoublesMatrix *duplicate = [[DoublesMatrix alloc] initWithSizeRows: self.rows columns: self.columns];

for(int i = 0; i < rows; i++) {
for(int j = 0; j < columns; j++) {
[duplicate setValueAtRow: i column: j value: [self getValueAtRow: i column: j]];
for (int i = 0; i < self.rows; i++) {
for (int j = 0; j < self.columns; j++) {
[duplicate setValueAtRow: i column: j value: [self valueAtRow: i column: j]];
}
}

return duplicate;
}

@end
2 changes: 1 addition & 1 deletion PolynomialRegression/PolynomialRegression.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
#import "DoublesMatrix.h"

@interface PolynomialRegression : NSObject
+ (NSMutableArray *) regressionWithXValues: (NSMutableArray *) xvals AndYValues: (NSMutableArray *) yvals PolynomialDegree: (int) p;
+ (NSMutableArray *)regressionWithXValues:(NSMutableArray *)xvals yValues:(NSMutableArray *)yvals polynomialDegree:(NSUInteger)p;
@end
Loading

0 comments on commit de3bbf9

Please sign in to comment.