ObjFW  Diff

Differences From Artifact [8e0af9d60e]:

To Artifact [9473f8f25c]:

  • File src/OFDoubleMatrix.m — part of check-in [e1e7ffa903] at 2011-09-22 23:25:42 on branch trunk — Exceptions are now autoreleased.

    This is safe as an "exception loop" can't happen, since if allocating
    an exception fails, it throws an OFAllocFailedException which is
    preallocated and can always be thrown.

    So, the worst case would be that an autorelease of an exception fails,
    triggering an OFOutOfMemoryException for which there is no memory,
    resulting in an OFAllocFailedException to be thrown. (user: js, size: 10175) [annotate] [blame] [check-ins using]


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
	return ret;
}

- init
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithRows: (size_t)rows_
       columns: (size_t)columns_
{
	self = [super init];

	@try {
		rows = rows_;
		columns = columns_;

		if (SIZE_MAX / rows < columns ||
		    SIZE_MAX / rows * columns < sizeof(double))
			@throw [OFOutOfRangeException
			    newWithClass: isa];

		if ((data = malloc(rows * columns * sizeof(double))) == NULL)
			@throw [OFOutOfMemoryException
			     newWithClass: isa
			    requestedSize: rows * columns * sizeof(double)];


		memset(data, 0, rows * columns * sizeof(double));

		if (rows == columns) {
			size_t i;

			for (i = 0; i < rows * columns; i += rows + 1)







|
|














|



|
|
>







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
	return ret;
}

- init
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException exceptionWithClass: c
						    selector: _cmd];
}

- initWithRows: (size_t)rows_
       columns: (size_t)columns_
{
	self = [super init];

	@try {
		rows = rows_;
		columns = columns_;

		if (SIZE_MAX / rows < columns ||
		    SIZE_MAX / rows * columns < sizeof(double))
			@throw [OFOutOfRangeException
			    exceptionWithClass: isa];

		if ((data = malloc(rows * columns * sizeof(double))) == NULL)
			@throw [OFOutOfMemoryException
			     exceptionWithClass: isa
				  requestedSize: rows * columns *
						 sizeof(double)];

		memset(data, 0, rows * columns * sizeof(double));

		if (rows == columns) {
			size_t i;

			for (i = 0; i < rows * columns; i += rows + 1)
135
136
137
138
139
140
141
142
143
144
145
146
147

148
149
150
151
152
153
154
		size_t i;

		rows = rows_;
		columns = columns_;

		if (SIZE_MAX / rows < columns ||
		    SIZE_MAX / rows * columns < sizeof(double))
			@throw [OFOutOfRangeException newWithClass: isa];

		if ((data = malloc(rows * columns * sizeof(double))) == NULL)
			@throw [OFOutOfMemoryException
			     newWithClass: isa
			    requestedSize: rows * columns * sizeof(double)];


		for (i = 0; i < rows; i++) {
			size_t j;

			for (j = i; j < rows * columns; j += rows)
				data[j] = (j == 0
				    ? data_ : va_arg(arguments, double));







|



|
|
>







136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
		size_t i;

		rows = rows_;
		columns = columns_;

		if (SIZE_MAX / rows < columns ||
		    SIZE_MAX / rows * columns < sizeof(double))
			@throw [OFOutOfRangeException exceptionWithClass: isa];

		if ((data = malloc(rows * columns * sizeof(double))) == NULL)
			@throw [OFOutOfMemoryException
			     exceptionWithClass: isa
				  requestedSize: rows * columns *
						 sizeof(double)];

		for (i = 0; i < rows; i++) {
			size_t j;

			for (j = i; j < rows * columns; j += rows)
				data[j] = (j == 0
				    ? data_ : va_arg(arguments, double));
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
}

- (void)setValue: (double)value
	  forRow: (size_t)row
	  column: (size_t)column
{
	if (row >= rows || column >= columns)
		@throw [OFOutOfRangeException newWithClass: isa];

	data[row * columns + column] = value;
}

- (double)valueForRow: (size_t)row
	       column: (size_t)column
{
	if (row >= rows || column >= columns)
		@throw [OFOutOfRangeException newWithClass: isa];

	return data[row * columns + column];
}

- (size_t)rows
{
	return rows;







|








|







171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
}

- (void)setValue: (double)value
	  forRow: (size_t)row
	  column: (size_t)column
{
	if (row >= rows || column >= columns)
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	data[row * columns + column] = value;
}

- (double)valueForRow: (size_t)row
	       column: (size_t)column
{
	if (row >= rows || column >= columns)
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	return data[row * columns + column];
}

- (size_t)rows
{
	return rows;
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318

- (void)addMatrix: (OFDoubleMatrix*)matrix
{
	size_t i;

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

	for (i = 0; i < rows * columns; i++)
		data[i] += matrix->data[i];
}

- (void)subtractMatrix: (OFDoubleMatrix*)matrix
{
	size_t i;

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

	for (i = 0; i < rows * columns; i++)
		data[i] -= matrix->data[i];
}


- (void)multiplyWithScalar: (double)scalar







|
|











|
|







292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320

- (void)addMatrix: (OFDoubleMatrix*)matrix
{
	size_t i;

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

	for (i = 0; i < rows * columns; i++)
		data[i] += matrix->data[i];
}

- (void)subtractMatrix: (OFDoubleMatrix*)matrix
{
	size_t i;

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

	for (i = 0; i < rows * columns; i++)
		data[i] -= matrix->data[i];
}


- (void)multiplyWithScalar: (double)scalar
333
334
335
336
337
338
339
340
341
342
343
344
345
346

347
348
349
350
351
352
353

- (void)multiplyWithMatrix: (OFDoubleMatrix*)matrix
{
	double *newData;
	size_t i, base1, base2;

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

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


	base1 = 0;
	base2 = 0;

	for (i = 0; i < columns; i++) {
		size_t base3 = base2;
		size_t j;







|
|



|
|
>







335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356

- (void)multiplyWithMatrix: (OFDoubleMatrix*)matrix
{
	double *newData;
	size_t i, base1, base2;

	if (rows != matrix->columns)
		@throw [OFInvalidArgumentException exceptionWithClass: isa
							     selector: _cmd];

	if ((newData = malloc(matrix->rows * columns * sizeof(double))) == NULL)
		@throw [OFOutOfMemoryException
		     exceptionWithClass: isa
			  requestedSize: matrix->rows * columns *
					 sizeof(double)];

	base1 = 0;
	base2 = 0;

	for (i = 0; i < columns; i++) {
		size_t base3 = base2;
		size_t j;
380
381
382
383
384
385
386
387

388
389
390
391
392
393
394
395
396

- (void)transpose
{
	double *newData;
	size_t i, k;

	if ((newData = malloc(rows * columns * sizeof(double))) == NULL)
		@throw [OFOutOfMemoryException newWithClass: isa

					      requestedSize: rows * columns *
							     sizeof(double)];

	rows ^= columns;
	columns ^= rows;
	rows ^= columns;

	for (i = k = 0; i < rows; i++) {
		size_t j;







|
>
|
<







383
384
385
386
387
388
389
390
391
392

393
394
395
396
397
398
399

- (void)transpose
{
	double *newData;
	size_t i, k;

	if ((newData = malloc(rows * columns * sizeof(double))) == NULL)
		@throw [OFOutOfMemoryException
		    exceptionWithClass: isa
			 requestedSize: rows * columns * sizeof(double)];


	rows ^= columns;
	columns ^= rows;
	rows ^= columns;

	for (i = k = 0; i < rows; i++) {
		size_t j;
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
- (void)translateWithVector: (OFDoubleVector*)vector
{
	OFDoubleMatrix *translation;
	double *cArray;

	if (rows != columns || vector->isa != doubleVector ||
	    vector->dimension != rows - 1)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	cArray = [vector cArray];
	translation = [[OFDoubleMatrix alloc] initWithRows: rows
						  columns: columns];

	memcpy(translation->data + (columns - 1) * rows, cArray,
	    (rows - 1) * sizeof(double));







|
|







409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
- (void)translateWithVector: (OFDoubleVector*)vector
{
	OFDoubleMatrix *translation;
	double *cArray;

	if (rows != columns || vector->isa != doubleVector ||
	    vector->dimension != rows - 1)
		@throw [OFInvalidArgumentException exceptionWithClass: isa
							     selector: _cmd];

	cArray = [vector cArray];
	translation = [[OFDoubleMatrix alloc] initWithRows: rows
						  columns: columns];

	memcpy(translation->data + (columns - 1) * rows, cArray,
	    (rows - 1) * sizeof(double));
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
		   angle: (double)angle
{
	OFDoubleMatrix *rotation;
	double n[3], m, angleCos, angleSin;

	if (rows != 4 || columns != 4 || vector->isa != doubleVector ||
	    vector->dimension != 3)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	n[0] = vector->data[0];
	n[1] = vector->data[1];
	n[2] = vector->data[2];

	m = sqrt(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);








|
|







434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
		   angle: (double)angle
{
	OFDoubleMatrix *rotation;
	double n[3], m, angleCos, angleSin;

	if (rows != 4 || columns != 4 || vector->isa != doubleVector ||
	    vector->dimension != 3)
		@throw [OFInvalidArgumentException exceptionWithClass: isa
							     selector: _cmd];

	n[0] = vector->data[0];
	n[1] = vector->data[1];
	n[2] = vector->data[2];

	m = sqrt(n[0] * n[0] + n[1] * n[1] + n[2] * n[2]);

480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
{
	OFDoubleMatrix *scale;
	double *cArray;
	size_t i, j;

	if (rows != columns || vector->isa != doubleVector ||
	    vector->dimension != rows - 1)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	cArray = [vector cArray];
	scale = [[OFDoubleMatrix alloc] initWithRows: rows
					    columns: columns];

	for (i = j = 0; i < ((rows - 1) * columns) - 1; i += rows + 1)
		scale->data[i] = cArray[j++];







|
|







483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
{
	OFDoubleMatrix *scale;
	double *cArray;
	size_t i, j;

	if (rows != columns || vector->isa != doubleVector ||
	    vector->dimension != rows - 1)
		@throw [OFInvalidArgumentException exceptionWithClass: isa
							     selector: _cmd];

	cArray = [vector cArray];
	scale = [[OFDoubleMatrix alloc] initWithRows: rows
					    columns: columns];

	for (i = j = 0; i < ((rows - 1) * columns) - 1; i += rows + 1)
		scale->data[i] = cArray[j++];