ObjFW  Diff

Differences From Artifact [5ca73ec320]:

To Artifact [b49ac3419e]:


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
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







+









+




+
+

+
+
+
+
+
+







 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <math.h>

#import "OFFloatVector.h"
#import "OFFloatMatrix.h"
#import "OFString.h"

#import "OFInvalidArgumentException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"

#import "macros.h"

static Class floatMatrix = Nil;

@implementation OFFloatVector
+ (void)initialize
{
	if (self == [OFFloatVector class])
		floatMatrix = [OFFloatMatrix class];
}

+ vectorWithDimension: (size_t)dimension
{
	return [[[self alloc] initWithDimension: dimension] autorelease];
}

+ vectorWithDimensionAndData: (size_t)dimension, ...
{
59
60
61
62
63
64
65


66
67





68
69
70
71
72
73
74
69
70
71
72
73
74
75
76
77


78
79
80
81
82
83
84
85
86
87
88
89







+
+
-
-
+
+
+
+
+







- initWithDimension: (size_t)dimension_
{
	self = [super init];

	@try {
		dimension = dimension_;

		if (SIZE_MAX / dimension < sizeof(float))
			@throw [OFOutOfRangeException newWithClass: isa];
		data = [self allocMemoryForNItems: dimension
					 withSize: sizeof(float)];

		if ((data = malloc(dimension * sizeof(float))) == NULL)
			@throw [OFOutOfMemoryException
			     newWithClass: isa
			    requestedSize: dimension * sizeof(float)];

		memset(data, 0, dimension * sizeof(float));
	} @catch (id e) {
		[self release];
		@throw e;
	}

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
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







+
+
-
-
+
+
+
+
+










+
+
+
+
+
+
+







	self = [super init];

	@try {
		size_t i;

		dimension = dimension_;

		if (SIZE_MAX / dimension < sizeof(float))
			@throw [OFOutOfRangeException newWithClass: isa];
		data = [self allocMemoryForNItems: dimension
					 withSize: sizeof(float)];

		if ((data = malloc(dimension * sizeof(float))) == NULL)
			@throw [OFOutOfMemoryException
			     newWithClass: isa
			    requestedSize: dimension * sizeof(float)];

		for (i = 0; i < dimension; i++)
			data[i] = (float)va_arg(arguments, double);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	free(data);

	[super dealloc];
}

- (void)setValue: (float)value
	 atIndex: (size_t)index
{
	if (index >= dimension)
		@throw [OFOutOfRangeException newWithClass: isa];

317
318
319
320
321
322
323
324
325

326
327
328
329

330
331
332
333
334
335




336
337
338
339

340
341
342


343
344

345
346
347
348
349
350

351
352
353

354
355
344
345
346
347
348
349
350


351
352
353


354

355
356
357


358
359
360
361
362



363
364


365
366
367

368
369
370
371
372
373

374
375
376

377
378
379







-
-
+


-
-
+
-



-
-
+
+
+
+

-
-
-
+

-
-
+
+

-
+





-
+


-
+



	for (i = 0; i < dimension; i++)
		data[i] /= magnitude;
}

- (void)multiplyWithMatrix: (OFFloatMatrix*)matrix
{
	size_t rows, columns;
	float *cArray, *newData;
	float *newData;
	size_t i, j, k;

	columns = [matrix columns];

	if (matrix->isa != floatMatrix || dimension != matrix->columns)
	if (dimension != columns)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	rows = [matrix rows];
	cArray = [matrix cArray];
	if ((newData = malloc(matrix->rows * sizeof(float))) == NULL)
		@throw [OFOutOfMemoryException
		     newWithClass: isa
		    requestedSize: matrix->rows * sizeof(float)];

	newData = [self allocMemoryForNItems: rows
				    withSize: sizeof(float)];
	memset(newData, 0, rows * sizeof(float));
	memset(newData, 0, matrix->rows * sizeof(float));

	for (i = j = k = 0; i < rows * columns; i++) {
		newData[j] += cArray[i] * data[k];
	for (i = j = k = 0; i < matrix->rows * matrix->columns; i++) {
		newData[j] += matrix->data[i] * data[k];

		if (++j == rows) {
		if (++j == matrix->rows) {
			k++;
			j = 0;
		}
	}

	[self freeMemory: data];
	free(data);
	data = newData;

	dimension = rows;
	dimension = matrix->rows;
}
@end