-
Notifications
You must be signed in to change notification settings - Fork 0
/
CWMatrix.m
228 lines (182 loc) · 4.9 KB
/
CWMatrix.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//
// CWMatrix.m
// MMCourseWork
//
// Created by Alexey Streltsow on 11/26/09.
// Copyright 2009 Karma World LLC. All rights reserved.
//
#import "CWMatrix.h"
#define VAL(i, j) _data[i*_columns + j]
#define VALM(m, i, j) m.data[i*m.columns+j]
@interface CWMatrix ()
@end
@implementation CWMatrix
@synthesize data = _data;
+ (id) matrixWithRows:(int)rows columns:(int)columns {
return [[[CWMatrix alloc] initWithRows:rows andColumns:columns] autorelease];
}
- (id) initWithRows:(int)rows andColumns:(int)columns {
if ( rows == 0 || columns == 0 ) {
[self release];
return nil;
}
if ( self = [super init] ) {
_columns = columns;
_rows = rows;
_data = calloc(columns*rows, sizeof(double));
}
return self;
}
- (id) initWithData:(double*)data rows:(int)rows columns:(int)columns {
if ( rows == 0 || columns == 0 ) {
[self release];
return nil;
}
if ( self = [super init] ) {
_columns = columns;
_rows = rows;
_data = calloc(columns*rows, sizeof(double));
memcpy(_data, data, _rows*_columns*sizeof(double));
}
return self;
}
- (id) copy {
CWMatrix* newMatrix = [[CWMatrix alloc] initWithRows:self.rows andColumns:self.columns];
assert(newMatrix);
memcpy(newMatrix.data, _data, _rows*_columns*sizeof(double));
return newMatrix;
}
#pragma mark Accessors
- (void) setValue:(double)value row:(int)row column:(int)column {
if ( column >= _columns || row >= _rows ) {
NSLog(@"SET Matrix value out of bounds %d, %d", row, column);
return;
}
_data[ row*_columns + column ] = value;
}
- (double) valueForRow:(int)row column:(int)column {
if ( column >= self.columns || row >= self.rows ) {
NSLog(@"GET Matrix value out of bounds %d, %d", row, column);
return NAN;
}
return _data[ row*_columns + column ];
}
#pragma mark Base Matrix Operations
- (CWMatrix*) addMatrix:(CWMatrix*)matrix {
CWMatrix* newMatrix = [self copy];
for (int i=0; i < MIN(_rows, matrix.rows); ++i)
for (int j=0; j < MIN(_columns, matrix.columns); ++j)
newMatrix.data[i*_columns + j] += matrix.data[i * matrix.columns + j];
return [newMatrix autorelease];
}
- (CWMatrix*) substractMatrix:(CWMatrix*)matrix {
CWMatrix* newMatrix = [self copy];
for (int i=0; i < MIN(_rows, matrix.rows); ++i)
for (int j=0; j < MIN(_columns, matrix.columns); ++j)
newMatrix.data[i*_columns + j] -= matrix.data[i * matrix.columns + j];
return [newMatrix autorelease];
}
- (CWMatrix*) multiplyByScalar:(double)scalar {
CWMatrix* newMatrix = [self copy];
for (int i=0; i<_rows; ++i)
for (int j=0; j<_columns; ++j)
newMatrix.data[i*_columns + j] *= scalar;
return [newMatrix autorelease];
}
- (CWMatrix*)multiplyByMatrix:(CWMatrix*)matrix{
if(_columns != matrix.rows){
return nil;
}
NSUInteger mColumns = matrix.columns;
CWMatrix* matris = [CWMatrix matrixWithRows:_rows columns:mColumns];
for(NSUInteger i = 0; i < _rows; i++) {
for(NSUInteger j = 0; j < mColumns; j++) {
double sum = 0.0;
for(NSUInteger k = 0; k < _columns; k++) {
//sum += [self valueForRow:i column:k] * [matrix valueForRow:k column:j];
sum += VAL(i,k) * VALM(matrix,k,j);
}
//[matris setValue:sum row:i column:j];
VALM(matris,i,j) = sum;
}
}
return matris;
}
#pragma mark submatrices
- (CWMatrix*) submatrixWithRows:(NSArray*)rows initialColumn:(int)j0 finalColumn:(int)j1 {
CWMatrix* X = [CWMatrix matrixWithRows:[rows count] columns:j1-j0+1];
@try {
for (int i=0; i < [rows count]; i++ ) {
for (int j = j0; j <= j1; j++ ) {
int row = [[rows objectAtIndex:i] intValue];
VALM(X, i, j-j0) = VAL(row, j);
}
}
}
@catch (NSException* e) {
X = nil;
}
@finally {
return X;
}
}
- (CWMatrix*) submatrixWithInitialRow:(int)i0 finalRow:(int)i1 initialColumn:(int)j0 finalColumn:(int)j1 {
CWMatrix* X = [CWMatrix matrixWithRows:i1-i0+1 columns:j1-j0+1];
@try {
for (int i = i0; i <= i1; i++) {
for (int j = j0; j <= j1; j++) {
VALM(X, i-i0, j-j0) = VAL(i,j);
}
}
}
@catch (NSException* e) {
X = nil;
}
@finally {
return X;
}
}
#pragma mark Properties
- (NSUInteger) rows {
return _rows;
}
- (NSUInteger) columns {
return _columns;
}
- (NSUInteger ) rank {
if ( !self.square )
return 0;
return self.rows;
}
- (BOOL) square {
return (self.rows == self.columns);
}
- (double*) copyAsArray {
double* array = calloc(self.columns*self.rows, sizeof(double));
memcpy(array, _data, self.columns*self.rows * sizeof(double) );
return array;
}
- (void) dealloc {
free(_data);
[super dealloc];
}
#pragma mark debug
- (NSString*)description{
NSMutableString* str = [NSMutableString new];
[str appendString:@"{"];
for(NSUInteger i = 0; i < _rows; i++){
[str appendString:@"{"];
for(NSUInteger j = 0; j < _columns; j++){
[str appendFormat:@"%g", [self valueForRow:i column:j]];
if(j+1 != _columns)
[str appendString:@","];
}
[str appendString:@"}"];
if(i+1 != _rows)
[str appendString:@",\n"];
}
[str appendString:@"}"];
[str autorelease];
return str;
}
@end