forked from natikgadzhi/XNMaths
-
Notifications
You must be signed in to change notification settings - Fork 1
/
XNMatrix.m
273 lines (214 loc) · 6.99 KB
/
XNMatrix.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//
// XNMatrix.m
// Assignation 3.2
//
// Created by Нат Гаджибалаев on 15.11.09.
// Copyright 2009 Нат Гаджибалаев. All rights reserved.
//
#pragma mark -
#pragma mark Imports
#import "XNMatrix.h"
#import "XNVector.h"
#pragma mark -
#pragma mark XNMatrix class implementation
@implementation XNMatrix
@synthesize rowsCount;
@synthesize columnsCount;
#pragma mark -
#pragma mark Class init methods
- (XNMatrix *) matrixWithRows: (NSUInteger) newRowsCount columns: (NSUInteger) newColumnsCount
{
return [[[XNMatrix alloc] initWithRows: newRowsCount columns: newColumnsCount] autorelease];
}
- (XNMatrix *) matrixWithRows: (NSUInteger) newRowsCount columns: (NSUInteger) newColumnsCount filledWith: ( CGFloat *) newCArray
{
return [[[XNMatrix alloc] initWithRows: newRowsCount columns: newColumnsCount filledWith: newCArray] autorelease];
}
#pragma mark -
#pragma mark Initialization methods.
- (XNMatrix *) initWithRows: (NSUInteger) newRowsCount columns: (NSUInteger) newColumnsCount
{
self = [super init];
rowsCount = newRowsCount;
columnsCount = newColumnsCount;
data = calloc( rowsCount * columnsCount, sizeof(CGFloat));
for( NSUInteger i = 0; i < rowsCount; i++ ){
for(NSUInteger j = 0; j < columnsCount; j++){
data[i * columnsCount + j] = 0.0f;
}
}
return self;
}
- (XNMatrix *) initWithRows: (NSUInteger) newRowsCount columns: (NSUInteger) newColumnsCount filledWith: ( CGFloat *) newCArray
{
self = [super init];
rowsCount = newRowsCount;
columnsCount = newColumnsCount;
data = calloc( rowsCount * columnsCount, sizeof(CGFloat));
for( NSUInteger i = 0; i < rowsCount; i++ ){
for(NSUInteger j = 0; j < columnsCount; j++){
data[i * columnsCount + j] = newCArray[i * columnsCount + j];
}
}
return self;
}
#pragma mark -
#pragma mark -
#pragma mark Accessor methods
- (CGFloat) valueAtRow: (NSUInteger)row column: (NSUInteger)column
{
if( row >= rowsCount || column >= columnsCount ){
[NSException raise:@"Out of dimensions error." format:@"Trying to get element [%d, %d] of [%d, %d] matrix.",
row, column, rowsCount, columnsCount];
}
return data[ (row * columnsCount) + column ];
}
- (void) setValue: (CGFloat)value atRow: (NSUInteger)row column: (NSUInteger)column
{
if( row >= rowsCount || column >= columnsCount ){
[NSException raise:@"Out of dimensions error." format:@"Trying to set element [%d, %d] of [%d, %d] matrix.",
row, column, rowsCount, columnsCount];
}
data[row * columnsCount + column] = value;
}
#pragma mark -
#pragma mark Object mgmt
- (XNMatrix *) copy
{
return [[ XNMatrix alloc] initWithRows: rowsCount columns: columnsCount filledWith: data];
}
#pragma mark -
#pragma mark Access columns and rows
- (XNVector *) columnVectorAtIndex: (NSUInteger) index
{
if(index >= columnsCount){
[NSException raise:@"Out of dimensions error." format:@"Trying to access %d column of [%d, %d] matrix.",
index, rowsCount, columnsCount];
}
CGFloat *columnData = calloc(rowsCount, sizeof(CGFloat));
for( NSUInteger i = 0; i < rowsCount; i++ ){
columnData[i] = data[i * columnsCount + index];
}
return [[XNVector alloc] initWithCapacity: rowsCount filledWith: columnData];
}
- (XNVector *) rowVectorAtIndex: (NSUInteger) index
{
if(index >= rowsCount){
[NSException raise:@"Out of dimensions error." format:@"Trying to access %d row of [%d, %d] matrix.",
index, rowsCount, columnsCount];
}
CGFloat *rowData = calloc(columnsCount, sizeof(CGFloat));
for( NSUInteger j = 0; j < columnsCount; j++ ){
rowData[j] = data[index * columnsCount + j];
}
return [[XNVector alloc] initWithCapacity: columnsCount filledWith: rowData];
}
//
// removeColumnAtIndex:
// Removes a column by index from the matrix.
//
- (void) removeColumnAtIndex: (NSUInteger) index
{
if( index >= columnsCount){
[NSException raise:@"Out of dimensions error." format:@"Trying to access %d column of [%d, %d] matrix.",
index, rowsCount, columnsCount];
}
if( columnsCount == 1){
[NSException raise: @"Can't remove row error." format: @"Can't remove a column in a single-column matrix."];
}
CGFloat *newData = calloc(rowsCount * (columnsCount - 1), sizeof(CGFloat));
for( NSUInteger j = 0; j < index; j++ ){
for( NSUInteger i = 0; i < rowsCount; i++ ){
newData[(i * (columnsCount - 1)) + j] = data[(i * columnsCount) + j];
}
}
for( NSUInteger j = index + 1; j < columnsCount; j++ ){
for( NSUInteger i = 0; i < rowsCount; i++ ){
newData[(i * (columnsCount-1)) + (j - 1)] = data[(i * columnsCount) + j];
}
}
free(data);
data = newData;
columnsCount--;
}
//
// removeRowAtIndex:
// Removes a row from the matrix.
//
- (void) removeRowAtIndex: (NSUInteger) index
{
if(index >= rowsCount){
[NSException raise:@"Out of dimensions error." format:@"Trying to access %d row of [%d, %d] matrix.",
index, rowsCount, columnsCount];
}
if( rowsCount == 1){
[NSException raise: @"Can't remove row error." format: @"Can't remove a row in a single-row matrix."];
}
//
// We can't just remove ane row since memory allocation is done at once.
// We'll create a new data array and release the one that exists now.
CGFloat *newData = calloc( (rowsCount - 1) * columnsCount, sizeof(CGFloat));
//
// Copy data before the removed row
for( NSUInteger i = 0; i < index; i++ ){
for( NSUInteger j = 0; j < columnsCount; j++ ){
newData[ (i * columnsCount) + j] = data[ (i * columnsCount) + j];
}
}
//
// Copy data after the removed row
for( NSUInteger i = index + 1; i < rowsCount; i++ ){
for( NSUInteger j = 0; j < columnsCount; j++ ){
newData[ ((i - 1) * columnsCount) + j] = data[ (i * columnsCount) + j];
}
}
free(data);
data = newData;
rowsCount--;
}
#pragma mark -
#pragma mark Matrix properties
- (BOOL) isSquare
{
return (columnsCount == rowsCount);
}
- (XNVector *) multiplyByVector: (XNVector *) vector
{
if( vector.capacity != columnsCount ){
[NSException raise: @"Matrix multiplication error." format: @"Can't multiply %d-dimentional vector with %d x %d matrix.",
vector.capacity,
columnsCount,
rowsCount];
}
XNVector *newVector = [[XNVector alloc] initWithCapacity: rowsCount];
for( NSInteger i = 0; i < rowsCount; i++){
CGFloat newValue = 0.;
for( NSInteger j = 0; j < columnsCount; j++){
newValue += [self valueAtRow: i column: j] * [vector valueAtIndex: j];
}
[newVector setValue: newValue atIndex: i];
}
return [newVector autorelease];
}
#pragma mark -
#pragma mark Debugging
- (void) printToLog
{
NSLog(@"Matrix of %d on %d elements.", rowsCount, columnsCount );
for(NSInteger i = 0; i < rowsCount; i++ ){
NSMutableString *logFormatForRow = [NSMutableString stringWithCapacity: 30];
for( NSInteger j = 0; j < columnsCount; j++ ){
//NSLog(@"%d x %d => %f", (i * columnsCount), j, data[(i * columnsCount) + j]);
[logFormatForRow appendString: [NSMutableString stringWithFormat: @"%1.3f, ", data[(i * columnsCount) + j]]];
}
NSLog(@"[%@]", logFormatForRow);
}
}
#pragma mark -
#pragma mark Dealloc
- (void) dealloc
{
free(data);
[super dealloc];
}
@end