1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
5737
|
12 |
"{ Package: 'stx:libbasic' }"
|
|
13 |
|
1
|
14 |
ArrayedCollection variableFloatSubclass:#FloatArray
|
1263
|
15 |
instanceVariableNames:''
|
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
|
18 |
category:'Collections-Arrayed'
|
1
|
19 |
!
|
|
20 |
|
88
|
21 |
!FloatArray class methodsFor:'documentation'!
|
|
22 |
|
|
23 |
copyright
|
|
24 |
"
|
|
25 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
1
|
26 |
All Rights Reserved
|
|
27 |
|
88
|
28 |
This software is furnished under a license and may be used
|
|
29 |
only in accordance with the terms of that license and with the
|
|
30 |
inclusion of the above copyright notice. This software may not
|
|
31 |
be provided or otherwise made available to, or used by, any
|
|
32 |
other person. No title to or ownership of the software is
|
|
33 |
hereby transferred.
|
|
34 |
"
|
|
35 |
!
|
1
|
36 |
|
88
|
37 |
documentation
|
|
38 |
"
|
|
39 |
FloatArrays store floats (and nothing else).
|
|
40 |
See documentation in DoubleArray for more information.
|
1263
|
41 |
|
4062
|
42 |
[memory requirements:]
|
|
43 |
OBJ-HEADER + (size * float-size)
|
|
44 |
|
1263
|
45 |
[See also:]
|
|
46 |
DoubleArray Array
|
1290
|
47 |
|
|
48 |
[author:]
|
|
49 |
Claus Gittinger
|
88
|
50 |
"
|
628
|
51 |
! !
|
|
52 |
|
5737
|
53 |
!FloatArray methodsFor:'arithmetic'!
|
|
54 |
|
|
55 |
* anObject
|
|
56 |
"return the product of the receiver and the argument.
|
|
57 |
The argument may either be a scalar or another vector"
|
|
58 |
|
|
59 |
^ self clone *= anObject
|
|
60 |
|
|
61 |
"
|
|
62 |
#(1 2 3 4) asFloatArray * 3
|
|
63 |
#(1 2 3 4) asFloatArray * #(1 2 3 4) asFloatArray
|
|
64 |
"
|
|
65 |
!
|
|
66 |
|
|
67 |
+ anObject
|
|
68 |
"return the sum of the receiver and the argument.
|
|
69 |
The argument may either be a scalar or another vector"
|
|
70 |
|
|
71 |
^ self clone += anObject
|
|
72 |
|
|
73 |
"
|
|
74 |
#(1 2 3 4) asFloatArray + 3
|
|
75 |
#(1 2 3 4) asFloatArray + #(1 2 3 4) asFloatArray
|
|
76 |
"
|
|
77 |
!
|
|
78 |
|
10578
|
79 |
- anObject
|
|
80 |
"return the difference of the receiver and the argument.
|
|
81 |
The argument may either be a scalar or another vector"
|
|
82 |
|
|
83 |
^ self clone -= anObject
|
|
84 |
|
|
85 |
"
|
|
86 |
#(1 2 3 4) asFloatArray - 3
|
|
87 |
#(1 2 3 4) asFloatArray - #(1 2 3 4) asFloatArray
|
|
88 |
"
|
|
89 |
|
|
90 |
"Created: / 30-05-2007 / 17:41:46 / cg"
|
|
91 |
!
|
|
92 |
|
10579
|
93 |
/ anObject
|
|
94 |
"return the division of the receiver and the argument.
|
|
95 |
The argument may either be a scalar or another vector"
|
|
96 |
|
|
97 |
^ self clone /= anObject
|
|
98 |
|
|
99 |
"
|
|
100 |
#(1 2 3 4) asFloatArray / 3
|
|
101 |
#(1 2 3 4) asFloatArray / #(1 2 3 4) asFloatArray
|
|
102 |
"
|
|
103 |
|
|
104 |
"Created: / 30-05-2007 / 17:46:05 / cg"
|
|
105 |
!
|
|
106 |
|
10580
|
107 |
abs
|
|
108 |
^ self clone primAbs
|
|
109 |
|
|
110 |
"Created: / 30-05-2007 / 17:47:50 / cg"
|
|
111 |
!
|
|
112 |
|
10561
|
113 |
negated
|
10580
|
114 |
^ self clone primNegated
|
|
115 |
|
|
116 |
"Modified: / 30-05-2007 / 17:51:47 / cg"
|
|
117 |
!
|
|
118 |
|
|
119 |
primAbs
|
|
120 |
"destructive absolute value of each element"
|
|
121 |
|
|
122 |
%{
|
|
123 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) {
|
|
124 |
int _sz = __floatArraySize(self);
|
|
125 |
int i;
|
|
126 |
float *_p = __FloatArrayInstPtr(self)->f_element;
|
|
127 |
|
|
128 |
/* how about inline-mmx-asm for this ... */
|
|
129 |
for (i=0; i<_sz; i++) {
|
|
130 |
float f = _p[i];
|
|
131 |
|
|
132 |
if (f < 0) {
|
|
133 |
_p[i] = -f;
|
|
134 |
}
|
|
135 |
}
|
|
136 |
RETURN (self);
|
|
137 |
}
|
|
138 |
%}.
|
|
139 |
1 to: self size do:[:i| self at: i put: (self at: i) abs].
|
|
140 |
|
|
141 |
"
|
|
142 |
|f|
|
|
143 |
|
|
144 |
f := FloatArray withAll:#(-1 2 -3 4 -5).
|
|
145 |
f abs.
|
|
146 |
f
|
|
147 |
"
|
|
148 |
|
|
149 |
"Created: / 30-05-2007 / 17:50:17 / cg"
|
10561
|
150 |
!
|
|
151 |
|
5737
|
152 |
primAddArray: floatArray
|
|
153 |
"add the vector argument into the receiver (destructive).
|
|
154 |
The argument must be another vector"
|
|
155 |
|
|
156 |
%{
|
8913
|
157 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0))
|
5737
|
158 |
&& __isFloats(floatArray)
|
8913
|
159 |
&& (__ClassInstPtr(__qClass(floatArray))->c_ninstvars == __mkSmallInteger(0))) {
|
5737
|
160 |
int _sz1 = __floatArraySize(self);
|
|
161 |
int _sz2 = __floatArraySize(floatArray);
|
|
162 |
int i;
|
|
163 |
float *_p1 = __FloatArrayInstPtr(self)->f_element;
|
|
164 |
float *_p2 = __FloatArrayInstPtr(floatArray)->f_element;
|
|
165 |
|
|
166 |
if (_sz2 >= _sz1) {
|
|
167 |
/* how about inline-mmx-asm for this ... */
|
|
168 |
for (i=0; i<_sz1; i++) {
|
|
169 |
_p1[i] += _p2[i];
|
|
170 |
}
|
|
171 |
}
|
|
172 |
RETURN (self);
|
|
173 |
}
|
|
174 |
%}.
|
|
175 |
1 to: self size do:[:i| self at: i put: (self at: i) + (floatArray at: i)].
|
|
176 |
|
|
177 |
"
|
|
178 |
|f1 f2|
|
|
179 |
|
|
180 |
f1 := FloatArray withAll:#(1 2 3 4 5).
|
|
181 |
f2 := FloatArray withAll:#(2 2 2 3 3).
|
|
182 |
f1 += f2.
|
|
183 |
f1
|
|
184 |
"
|
|
185 |
!
|
|
186 |
|
|
187 |
primAddScalar: aScalar
|
|
188 |
"add the scalar argument into the receiver (destructive)."
|
|
189 |
|
|
190 |
%{
|
8913
|
191 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) {
|
5737
|
192 |
int _sz1 = __floatArraySize(self);
|
|
193 |
int i;
|
|
194 |
float *_p1 = __FloatArrayInstPtr(self)->f_element;
|
|
195 |
float v;
|
|
196 |
|
|
197 |
if (__isFloat(aScalar)) {
|
|
198 |
v = (float)(__floatVal(aScalar));
|
|
199 |
} else if (__isShortFloat(aScalar)) {
|
|
200 |
v = __shortFloatVal(aScalar);
|
|
201 |
} else if (__isSmallInteger(aScalar)) {
|
|
202 |
v = (float)(__intVal(aScalar));
|
|
203 |
} else
|
|
204 |
goto badArg;
|
|
205 |
|
|
206 |
/* how about inline-mmx-asm for this ... */
|
|
207 |
for (i=0; i<_sz1; i++) {
|
|
208 |
_p1[i] += v;
|
|
209 |
}
|
|
210 |
RETURN (self);
|
|
211 |
}
|
|
212 |
badArg: ;
|
|
213 |
%}.
|
|
214 |
1 to: self size do:[:i| self at: i put: (self at: i) + aScalar].
|
|
215 |
|
|
216 |
"
|
|
217 |
|f1 f2|
|
|
218 |
|
|
219 |
f1 := FloatArray withAll:#(1 2 3 4 5).
|
|
220 |
f1 += 2.0.
|
|
221 |
Transcript showCR:f1.
|
|
222 |
f1 += 2.0 asShortFloat.
|
|
223 |
Transcript showCR:f1.
|
|
224 |
f1 += 2.
|
|
225 |
Transcript showCR:f1.
|
|
226 |
"
|
|
227 |
!
|
|
228 |
|
10561
|
229 |
primDivArray: floatArray
|
|
230 |
"divide the vector argument into the receiver (destructive).
|
|
231 |
The argument must be another vector"
|
|
232 |
|
|
233 |
%{
|
|
234 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0))
|
|
235 |
&& __isFloats(floatArray)
|
|
236 |
&& (__ClassInstPtr(__qClass(floatArray))->c_ninstvars == __mkSmallInteger(0))) {
|
|
237 |
int _sz1 = __floatArraySize(self);
|
|
238 |
int _sz2 = __floatArraySize(floatArray);
|
|
239 |
int i;
|
|
240 |
float *_p1 = __FloatArrayInstPtr(self)->f_element;
|
|
241 |
float *_p2 = __FloatArrayInstPtr(floatArray)->f_element;
|
|
242 |
|
|
243 |
if (_sz2 >= _sz1) {
|
|
244 |
/* how about inline-mmx-asm for this ... */
|
|
245 |
for (i=0; i<_sz1; i++) {
|
|
246 |
_p1[i] /= _p2[i];
|
|
247 |
}
|
|
248 |
}
|
|
249 |
RETURN (self);
|
|
250 |
}
|
|
251 |
%}.
|
|
252 |
1 to: self size do:[:i| self at: i put: (self at: i) / (floatArray at: i)].
|
|
253 |
|
|
254 |
"
|
|
255 |
|f1 f2|
|
|
256 |
|
|
257 |
f1 := FloatArray withAll:#(1 2 3 4 5).
|
|
258 |
f2 := FloatArray withAll:#(2 2 2 3 3).
|
|
259 |
f1 /= f2.
|
|
260 |
f1
|
|
261 |
"
|
|
262 |
|
|
263 |
"Modified: / 29-05-2007 / 16:01:34 / cg"
|
|
264 |
!
|
|
265 |
|
|
266 |
primDivScalar: aScalar
|
|
267 |
"divide the scalar argument into the receiver (destructive)."
|
|
268 |
|
|
269 |
%{
|
|
270 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) {
|
|
271 |
int _sz1 = __floatArraySize(self);
|
|
272 |
int i;
|
|
273 |
float *_p1 = __FloatArrayInstPtr(self)->f_element;
|
|
274 |
float v;
|
|
275 |
|
|
276 |
if (__isFloat(aScalar)) {
|
|
277 |
v = (float)(__floatVal(aScalar));
|
|
278 |
} else if (__isShortFloat(aScalar)) {
|
|
279 |
v = __shortFloatVal(aScalar);
|
|
280 |
} else if (__isSmallInteger(aScalar)) {
|
|
281 |
v = (float)(__intVal(aScalar));
|
|
282 |
} else
|
|
283 |
goto badArg;
|
|
284 |
|
|
285 |
/* how about inline-mmx-asm for this ... */
|
|
286 |
for (i=0; i<_sz1; i++) {
|
|
287 |
_p1[i] /= v;
|
|
288 |
}
|
|
289 |
RETURN (self);
|
|
290 |
}
|
|
291 |
badArg: ;
|
|
292 |
%}.
|
|
293 |
1 to: self size do:[:i| self at: i put: (self at: i) / aScalar].
|
|
294 |
|
|
295 |
"
|
|
296 |
|f1 f2|
|
|
297 |
|
|
298 |
f1 := FloatArray withAll:#(1 2 3 4 5).
|
|
299 |
f1 /= 2.0.
|
|
300 |
Transcript showCR:f1.
|
|
301 |
f1 /= 2.0 asShortFloat.
|
|
302 |
Transcript showCR:f1.
|
|
303 |
f1 /= 2.
|
|
304 |
Transcript showCR:f1.
|
|
305 |
"
|
|
306 |
|
|
307 |
"Modified: / 29-05-2007 / 16:01:39 / cg"
|
|
308 |
!
|
|
309 |
|
5737
|
310 |
primMulArray: floatArray
|
|
311 |
"multiply the vector argument into the receiver (destructive).
|
|
312 |
The argument must be another vector"
|
|
313 |
|
|
314 |
%{
|
8913
|
315 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0))
|
5737
|
316 |
&& __isFloats(floatArray)
|
8913
|
317 |
&& (__ClassInstPtr(__qClass(floatArray))->c_ninstvars == __mkSmallInteger(0))) {
|
5737
|
318 |
int _sz1 = __floatArraySize(self);
|
|
319 |
int _sz2 = __floatArraySize(floatArray);
|
|
320 |
int i;
|
|
321 |
float *_p1 = __FloatArrayInstPtr(self)->f_element;
|
|
322 |
float *_p2 = __FloatArrayInstPtr(floatArray)->f_element;
|
|
323 |
|
|
324 |
if (_sz2 >= _sz1) {
|
|
325 |
/* how about inline-mmx-asm for this ... */
|
|
326 |
for (i=0; i<_sz1; i++) {
|
|
327 |
_p1[i] *= _p2[i];
|
|
328 |
}
|
|
329 |
}
|
|
330 |
RETURN (self);
|
|
331 |
}
|
|
332 |
%}.
|
|
333 |
1 to: self size do:[:i| self at: i put: (self at: i) * (floatArray at: i)].
|
|
334 |
|
|
335 |
"
|
|
336 |
|f1 f2|
|
|
337 |
|
|
338 |
f1 := FloatArray withAll:#(1 2 3 4 5).
|
|
339 |
f2 := FloatArray withAll:#(2 2 2 3 3).
|
|
340 |
f1 *= f2.
|
|
341 |
f1
|
|
342 |
"
|
|
343 |
!
|
|
344 |
|
|
345 |
primMulScalar: aScalar
|
|
346 |
"multiply the scalar argument into the receiver (destructive)."
|
|
347 |
|
|
348 |
%{
|
8913
|
349 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) {
|
5737
|
350 |
int _sz1 = __floatArraySize(self);
|
|
351 |
int i;
|
|
352 |
float *_p1 = __FloatArrayInstPtr(self)->f_element;
|
|
353 |
float v;
|
|
354 |
|
|
355 |
if (__isFloat(aScalar)) {
|
|
356 |
v = (float)(__floatVal(aScalar));
|
|
357 |
} else if (__isShortFloat(aScalar)) {
|
|
358 |
v = __shortFloatVal(aScalar);
|
|
359 |
} else if (__isSmallInteger(aScalar)) {
|
|
360 |
v = (float)(__intVal(aScalar));
|
|
361 |
} else
|
|
362 |
goto badArg;
|
|
363 |
|
|
364 |
/* how about inline-mmx-asm for this ... */
|
|
365 |
for (i=0; i<_sz1; i++) {
|
|
366 |
_p1[i] *= v;
|
|
367 |
}
|
|
368 |
RETURN (self);
|
|
369 |
}
|
|
370 |
badArg: ;
|
|
371 |
%}.
|
|
372 |
1 to: self size do:[:i| self at: i put: (self at: i) * aScalar].
|
|
373 |
|
|
374 |
"
|
|
375 |
|f1 f2|
|
|
376 |
|
|
377 |
f1 := FloatArray withAll:#(1 2 3 4 5).
|
|
378 |
f1 *= 2.0.
|
|
379 |
Transcript showCR:f1.
|
|
380 |
f1 *= 2.0 asShortFloat.
|
|
381 |
Transcript showCR:f1.
|
|
382 |
f1 *= 2.
|
|
383 |
Transcript showCR:f1.
|
|
384 |
"
|
10578
|
385 |
!
|
|
386 |
|
10580
|
387 |
primNegated
|
|
388 |
"destructive negative value of each element"
|
|
389 |
|
|
390 |
%{
|
|
391 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) {
|
|
392 |
int _sz = __floatArraySize(self);
|
|
393 |
int i;
|
|
394 |
float *_p = __FloatArrayInstPtr(self)->f_element;
|
|
395 |
|
|
396 |
/* how about inline-mmx-asm for this ... */
|
|
397 |
for (i=0; i<_sz; i++) {
|
|
398 |
float f = _p[i];
|
|
399 |
|
|
400 |
_p[i] = -f;
|
|
401 |
}
|
|
402 |
RETURN (self);
|
|
403 |
}
|
|
404 |
%}.
|
|
405 |
1 to: self size do:[:i| self at: i put: (self at: i) negated].
|
|
406 |
|
|
407 |
"
|
|
408 |
|f|
|
|
409 |
|
|
410 |
f := FloatArray withAll:#(-1 2 -3 4 -5).
|
|
411 |
f negated.
|
|
412 |
f
|
|
413 |
"
|
|
414 |
|
|
415 |
"Created: / 30-05-2007 / 17:51:29 / cg"
|
|
416 |
!
|
|
417 |
|
10578
|
418 |
primSubtractArray: floatArray
|
|
419 |
"subtract the vector argument from the receiver (destructive).
|
|
420 |
The argument must be another vector"
|
|
421 |
|
|
422 |
%{
|
|
423 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0))
|
|
424 |
&& __isFloats(floatArray)
|
|
425 |
&& (__ClassInstPtr(__qClass(floatArray))->c_ninstvars == __mkSmallInteger(0))) {
|
|
426 |
int _sz1 = __floatArraySize(self);
|
|
427 |
int _sz2 = __floatArraySize(floatArray);
|
|
428 |
int i;
|
|
429 |
float *_p1 = __FloatArrayInstPtr(self)->f_element;
|
|
430 |
float *_p2 = __FloatArrayInstPtr(floatArray)->f_element;
|
|
431 |
|
|
432 |
if (_sz2 >= _sz1) {
|
|
433 |
/* how about inline-mmx-asm for this ... */
|
|
434 |
for (i=0; i<_sz1; i++) {
|
|
435 |
_p1[i] -= _p2[i];
|
|
436 |
}
|
|
437 |
}
|
|
438 |
RETURN (self);
|
|
439 |
}
|
|
440 |
%}.
|
|
441 |
1 to: self size do:[:i| self at: i put: (self at: i) - (floatArray at: i)].
|
|
442 |
|
|
443 |
"
|
|
444 |
|f1 f2|
|
|
445 |
|
|
446 |
f1 := FloatArray withAll:#(1 2 3 4 5).
|
|
447 |
f2 := FloatArray withAll:#(2 2 2 3 3).
|
|
448 |
f1 -= f2.
|
|
449 |
f1
|
|
450 |
"
|
|
451 |
|
|
452 |
"Created: / 30-05-2007 / 17:42:41 / cg"
|
|
453 |
!
|
|
454 |
|
|
455 |
primSubtractScalar: aScalar
|
|
456 |
"subtract the scalar argument from the receiver (destructive)."
|
|
457 |
|
|
458 |
%{
|
|
459 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) {
|
|
460 |
int _sz1 = __floatArraySize(self);
|
|
461 |
int i;
|
|
462 |
float *_p1 = __FloatArrayInstPtr(self)->f_element;
|
|
463 |
float v;
|
|
464 |
|
|
465 |
if (__isFloat(aScalar)) {
|
|
466 |
v = (float)(__floatVal(aScalar));
|
|
467 |
} else if (__isShortFloat(aScalar)) {
|
|
468 |
v = __shortFloatVal(aScalar);
|
|
469 |
} else if (__isSmallInteger(aScalar)) {
|
|
470 |
v = (float)(__intVal(aScalar));
|
|
471 |
} else
|
|
472 |
goto badArg;
|
|
473 |
|
|
474 |
/* how about inline-mmx-asm for this ... */
|
|
475 |
for (i=0; i<_sz1; i++) {
|
|
476 |
_p1[i] -= v;
|
|
477 |
}
|
|
478 |
RETURN (self);
|
|
479 |
}
|
|
480 |
badArg: ;
|
|
481 |
%}.
|
|
482 |
1 to: self size do:[:i| self at: i put: (self at: i) - aScalar].
|
|
483 |
|
|
484 |
"
|
|
485 |
|f1 f2|
|
|
486 |
|
|
487 |
f1 := FloatArray withAll:#(1 2 3 4 5).
|
|
488 |
f1 -= 2.0.
|
|
489 |
Transcript showCR:f1.
|
|
490 |
f1 -= 2.0 asShortFloat.
|
|
491 |
Transcript showCR:f1.
|
|
492 |
f1 -= 2.
|
|
493 |
Transcript showCR:f1.
|
|
494 |
"
|
|
495 |
|
|
496 |
"Created: / 30-05-2007 / 17:43:06 / cg"
|
5737
|
497 |
! !
|
|
498 |
|
17736
|
499 |
!FloatArray methodsFor:'arithmetic destructive'!
|
|
500 |
|
|
501 |
*= anObject
|
|
502 |
"multiply the argument into the receiver (destructive).
|
|
503 |
The argument may either be a scalar or another vector"
|
|
504 |
|
|
505 |
^ anObject isNumber
|
|
506 |
ifTrue:[self primMulScalar: anObject asFloat]
|
|
507 |
ifFalse:[self primMulArray: anObject]
|
|
508 |
|
|
509 |
"
|
|
510 |
|f|
|
|
511 |
|
|
512 |
f := #(1 2 3 4) asFloatArray.
|
|
513 |
f *= 3.
|
|
514 |
f
|
|
515 |
"
|
|
516 |
"
|
|
517 |
|f|
|
|
518 |
|
|
519 |
f := #(1 2 3 4) asFloatArray.
|
|
520 |
f *= #(1 2 3 4) asFloatArray.
|
|
521 |
f
|
|
522 |
"
|
|
523 |
!
|
|
524 |
|
|
525 |
+= anObject
|
|
526 |
"add the argument into the receiver (destructive).
|
|
527 |
The argument may either be a scalar or another vector"
|
|
528 |
|
|
529 |
^ anObject isNumber
|
|
530 |
ifTrue:[self primAddScalar: anObject asFloat]
|
|
531 |
ifFalse:[self primAddArray: anObject]
|
|
532 |
|
|
533 |
"
|
|
534 |
|f|
|
|
535 |
|
|
536 |
f := #(1 2 3 4) asFloatArray.
|
|
537 |
f += 3.
|
|
538 |
f
|
|
539 |
"
|
|
540 |
"
|
|
541 |
|f|
|
|
542 |
|
|
543 |
f := #(1 2 3 4) asFloatArray.
|
|
544 |
f += #(1 2 3 4) asFloatArray.
|
|
545 |
f
|
|
546 |
"
|
|
547 |
!
|
|
548 |
|
|
549 |
-= anObject
|
|
550 |
"subtract the argument from the receiver (destructive).
|
|
551 |
The argument may either be a scalar or another vector"
|
|
552 |
|
|
553 |
^ anObject isNumber
|
|
554 |
ifTrue:[self primSubtractScalar: anObject asFloat]
|
|
555 |
ifFalse:[self primSubtractArray: anObject]
|
|
556 |
|
|
557 |
"
|
|
558 |
|f|
|
|
559 |
|
|
560 |
f := #(1 2 3 4) asFloatArray.
|
|
561 |
f -= 3.
|
|
562 |
f
|
|
563 |
"
|
|
564 |
"
|
|
565 |
|f|
|
|
566 |
|
|
567 |
f := #(1 2 3 4) asFloatArray.
|
|
568 |
f += #(1 2 3 4) asFloatArray.
|
|
569 |
f
|
|
570 |
"
|
|
571 |
|
|
572 |
"Created: / 30-05-2007 / 17:42:13 / cg"
|
|
573 |
!
|
|
574 |
|
|
575 |
/= anObject
|
|
576 |
"divide the argument into the receiver (destructive).
|
|
577 |
The argument may either be a scalar or another vector"
|
|
578 |
|
|
579 |
^ anObject isNumber
|
|
580 |
ifTrue:[self primDivScalar: anObject asFloat]
|
|
581 |
ifFalse:[self primDivArray: anObject]
|
|
582 |
|
|
583 |
"Modified: / 30-05-2007 / 17:45:46 / cg"
|
|
584 |
! !
|
|
585 |
|
5737
|
586 |
!FloatArray methodsFor:'copying'!
|
|
587 |
|
|
588 |
clone
|
|
589 |
"return a copy of the receiver"
|
|
590 |
|
|
591 |
|newArr|
|
|
592 |
|
|
593 |
newArr := self class new:(self size).
|
|
594 |
%{
|
8913
|
595 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) {
|
5737
|
596 |
int _sz = __floatArraySize(self);
|
|
597 |
|
|
598 |
bcopy(__FloatArrayInstPtr(self)->f_element,
|
|
599 |
__FloatArrayInstPtr(newArr)->f_element,
|
|
600 |
sizeof(float) * _sz);
|
|
601 |
|
|
602 |
RETURN (newArr);
|
|
603 |
}
|
|
604 |
%}.
|
|
605 |
newArr replaceFrom:1 to:self size with:self startingAt:1.
|
|
606 |
^ newArr
|
|
607 |
|
|
608 |
"
|
|
609 |
|f1 f2|
|
|
610 |
|
|
611 |
f1 := FloatArray withAll:#(1 2 3 4 5).
|
|
612 |
f1 clone
|
|
613 |
"
|
|
614 |
!
|
|
615 |
|
|
616 |
copyFrom:start to:stop
|
|
617 |
"return a partial copy of the receiver"
|
|
618 |
|
|
619 |
%{
|
8913
|
620 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) {
|
5737
|
621 |
if (__bothSmallInteger(start, stop)) {
|
|
622 |
int __start = __intVal(start) - 1;
|
|
623 |
int __stop = __intVal(stop) - 1 ;
|
|
624 |
|
|
625 |
if (__stop >= __start) {
|
|
626 |
int __sz = __floatArraySize(self);
|
|
627 |
|
|
628 |
if (((unsigned)__start < __sz)
|
|
629 |
&& ((unsigned)__stop < __sz)) {
|
|
630 |
int __n = __stop - __start + 1;
|
|
631 |
OBJ __nObj;
|
|
632 |
|
|
633 |
__nObj = __STX___new(sizeof(struct __FloatArray) + (__n - 1) * sizeof(float));
|
|
634 |
if (__nObj != nil) {
|
|
635 |
__objPtr(__nObj)->o_class = __qClass(self);
|
|
636 |
__STORE(__nObj, __qClass(self));
|
|
637 |
bcopy(__FloatArrayInstPtr(self)->f_element + __start,
|
|
638 |
__FloatArrayInstPtr(__nObj)->f_element,
|
|
639 |
sizeof(float) * __n);
|
|
640 |
RETURN(__nObj);
|
|
641 |
}
|
|
642 |
}
|
|
643 |
}
|
|
644 |
}
|
|
645 |
}
|
|
646 |
%}.
|
|
647 |
^ super copyFrom:start to:stop
|
|
648 |
|
|
649 |
"
|
|
650 |
|f1 f2|
|
|
651 |
|
|
652 |
f1 := FloatArray withAll:#(1 2 3 4 5 6).
|
|
653 |
f2 := f1 copyFrom:2 to:4.
|
|
654 |
f2
|
|
655 |
"
|
|
656 |
!
|
|
657 |
|
|
658 |
replaceFrom:start to:stop with:aCollection startingAt:replStart
|
|
659 |
%{
|
8913
|
660 |
if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0))
|
5737
|
661 |
&& __isFloats(aCollection)
|
8913
|
662 |
&& (__ClassInstPtr(__qClass(aCollection))->c_ninstvars == __mkSmallInteger(0))) {
|
5737
|
663 |
if (__bothSmallInteger(start, stop)
|
|
664 |
&& __isSmallInteger(replStart)) {
|
|
665 |
int __start = __intVal(start) - 1;
|
|
666 |
int __stop = __intVal(stop) - 1 ;
|
|
667 |
int __replStart = __intVal(replStart) - 1 ;
|
|
668 |
|
|
669 |
if (__stop >= __start) {
|
|
670 |
int __sz = __floatArraySize(self);
|
|
671 |
int __otherSz = __floatArraySize(aCollection);
|
|
672 |
int __replStop = __replStart + (__stop-__start);
|
|
673 |
|
|
674 |
if (((unsigned)__start < __sz)
|
|
675 |
&& ((unsigned)__stop < __sz)
|
|
676 |
&& ((unsigned)__replStart < __otherSz)
|
|
677 |
&& ((unsigned)__replStop < __otherSz)) {
|
|
678 |
int __n = __stop - __start + 1;
|
|
679 |
|
5773
|
680 |
if (aCollection != self) {
|
|
681 |
bcopy(&(__FloatArrayInstPtr(aCollection)->f_element[__replStart]),
|
|
682 |
&(__FloatArrayInstPtr(self)->f_element[__start]),
|
|
683 |
sizeof(float) * __n);
|
|
684 |
RETURN(self);
|
|
685 |
}
|
5737
|
686 |
}
|
|
687 |
}
|
|
688 |
}
|
|
689 |
}
|
|
690 |
%}.
|
|
691 |
^ super replaceFrom:start to:stop with:aCollection startingAt:replStart
|
|
692 |
|
|
693 |
"
|
|
694 |
|f1 f2|
|
|
695 |
|
|
696 |
f1 := (1 to:5) asFloatArray.
|
|
697 |
f2 := #(10 9 8 7 6) asFloatArray.
|
|
698 |
f1 replaceFrom:1 to:3 with:f2 startingAt:3
|
|
699 |
"
|
|
700 |
! !
|
|
701 |
|
17845
|
702 |
|
|
703 |
|
5737
|
704 |
!FloatArray methodsFor:'queries'!
|
|
705 |
|
5768
|
706 |
absMax
|
|
707 |
"return the largest absolute value"
|
|
708 |
|
5816
|
709 |
|mm|
|
5768
|
710 |
|
|
711 |
mm := self minMax.
|
|
712 |
^ (mm at:1) abs max:(mm at:2) abs
|
|
713 |
|
|
714 |
"
|
|
715 |
|f1|
|
|
716 |
|
|
717 |
f1 := (1 to:1000) asFloatArray.
|
|
718 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 absMax ] ]
|
|
719 |
"
|
11212
|
720 |
|
5768
|
721 |
"
|
|
722 |
|f1|
|
|
723 |
|
|
724 |
f1 := FloatArray withAll:#(1 2 3 4 5).
|
|
725 |
f1 absMax
|
|
726 |
"
|
|
727 |
"
|
|
728 |
|f1|
|
|
729 |
|
|
730 |
f1 := FloatArray withAll:#(5 4 3 2 1).
|
|
731 |
f1 absMax
|
|
732 |
"
|
|
733 |
"
|
|
734 |
|f1|
|
|
735 |
|
|
736 |
f1 := FloatArray withAll:#(5 -4 3 2 1).
|
|
737 |
f1 absMax
|
|
738 |
"
|
|
739 |
"
|
|
740 |
|f1|
|
|
741 |
|
|
742 |
f1 := FloatArray withAll:#(5 -5 3 2 1).
|
|
743 |
f1 absMax
|
|
744 |
"
|
|
745 |
"
|
|
746 |
|f1|
|
|
747 |
|
|
748 |
f1 := FloatArray withAll:#(5 -6 3 2 1).
|
|
749 |
f1 absMax
|
|
750 |
"
|
|
751 |
!
|
|
752 |
|
7220
|
753 |
defaultElement
|
|
754 |
^ ShortFloat zero
|
|
755 |
!
|
|
756 |
|
10561
|
757 |
length
|
17736
|
758 |
"Return the length of the receiver interpreted as vector
|
|
759 |
(that is the length of the vector from 0.0 @ 0.0 @ ... @ 0.0
|
11212
|
760 |
to the point in the n-dimensional space represented by the receiver)"
|
|
761 |
|
|
762 |
^ self squaredLength sqrt
|
|
763 |
|
|
764 |
"
|
|
765 |
#(10.0 10.0) asFloatArray length
|
|
766 |
#(10.0 10.0 10.0) asFloatArray length
|
|
767 |
"
|
10561
|
768 |
!
|
|
769 |
|
5737
|
770 |
max
|
|
771 |
"return the largest element;
|
|
772 |
redefined for speed"
|
|
773 |
%{
|
8913
|
774 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) {
|
7220
|
775 |
int _sz = __floatArraySize(self);
|
5737
|
776 |
|
7220
|
777 |
if (_sz > 0) {
|
|
778 |
float *_p = __FloatArrayInstPtr(self)->f_element;
|
|
779 |
float _max;
|
5737
|
780 |
|
7220
|
781 |
_max = _p[0];
|
|
782 |
if (_sz > 1) {
|
|
783 |
int _i;
|
|
784 |
float _prev, _this;
|
5737
|
785 |
|
7220
|
786 |
/* how about inline-mmx-asm for this ... */
|
|
787 |
_this = _p[1];
|
|
788 |
for (_i=2; _i<_sz; _i++) {
|
|
789 |
_prev = _this;
|
|
790 |
_this = _p[_i];
|
|
791 |
if (_prev > _max) _max = _prev;
|
5737
|
792 |
}
|
7220
|
793 |
if (_this > _max) _max = _this;
|
5737
|
794 |
}
|
7220
|
795 |
RETURN (__MKFLOAT(_max));
|
5737
|
796 |
}
|
7220
|
797 |
}
|
5737
|
798 |
%}.
|
7220
|
799 |
^ super max
|
5737
|
800 |
|
7220
|
801 |
"
|
|
802 |
|f1|
|
5737
|
803 |
|
7220
|
804 |
f1 := (1 to:1000) asFloatArray.
|
|
805 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 max ] ]
|
|
806 |
"
|
|
807 |
"
|
|
808 |
|f1|
|
5737
|
809 |
|
7220
|
810 |
f1 := FloatArray withAll:#(1 2 3 4 5).
|
|
811 |
f1 max
|
|
812 |
"
|
|
813 |
"
|
|
814 |
|f1|
|
5737
|
815 |
|
7220
|
816 |
f1 := FloatArray withAll:#(5 4 3 2 1).
|
|
817 |
f1 max
|
|
818 |
"
|
5737
|
819 |
!
|
|
820 |
|
|
821 |
min
|
|
822 |
"return the largest element;
|
|
823 |
redefined for speed"
|
|
824 |
%{
|
8913
|
825 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) {
|
7220
|
826 |
int _sz = __floatArraySize(self);
|
5737
|
827 |
|
7220
|
828 |
if (_sz > 0) {
|
|
829 |
float *_p = __FloatArrayInstPtr(self)->f_element;
|
|
830 |
float _min;
|
5737
|
831 |
|
7220
|
832 |
_min = _p[0];
|
|
833 |
if (_sz > 1) {
|
|
834 |
int _i;
|
|
835 |
float _prev, _this;
|
5737
|
836 |
|
7220
|
837 |
/* how about inline-mmx-asm for this ... */
|
|
838 |
_this = _p[1];
|
|
839 |
for (_i=2; _i<_sz; _i++) {
|
|
840 |
_prev = _this;
|
|
841 |
_this = _p[_i];
|
|
842 |
if (_prev < _min) _min = _prev;
|
5737
|
843 |
}
|
7220
|
844 |
if (_this < _min) _min = _this;
|
5737
|
845 |
}
|
7220
|
846 |
RETURN (__MKFLOAT(_min));
|
5737
|
847 |
}
|
7220
|
848 |
}
|
5737
|
849 |
%}.
|
7220
|
850 |
^ super min
|
5737
|
851 |
|
7220
|
852 |
"
|
|
853 |
|f1|
|
5737
|
854 |
|
7220
|
855 |
f1 := (1 to:1000) asFloatArray.
|
|
856 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 min ] ]
|
|
857 |
"
|
|
858 |
"
|
|
859 |
|f1|
|
5737
|
860 |
|
7220
|
861 |
f1 := FloatArray withAll:#(1 2 3 4 5).
|
|
862 |
f1 min
|
|
863 |
"
|
|
864 |
"
|
|
865 |
|f1|
|
5737
|
866 |
|
7220
|
867 |
f1 := FloatArray withAll:#(5 4 3 2 1).
|
|
868 |
f1 min
|
|
869 |
"
|
5737
|
870 |
!
|
|
871 |
|
|
872 |
minMax
|
11212
|
873 |
"return a Tuple holding the smallest and largest element;
|
5737
|
874 |
redefined for speed"
|
|
875 |
|
|
876 |
|min max|
|
|
877 |
|
|
878 |
%{
|
8913
|
879 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) {
|
7220
|
880 |
int _sz = __floatArraySize(self);
|
|
881 |
if (_sz > 0) {
|
|
882 |
int _i;
|
|
883 |
float *_p = __FloatArrayInstPtr(self)->f_element;
|
|
884 |
float _min, _max;
|
5737
|
885 |
|
7220
|
886 |
_min = _max = _p[0];
|
5737
|
887 |
|
7220
|
888 |
if (_sz > 1) {
|
|
889 |
float _this = _p[1];
|
|
890 |
float _prev;
|
|
891 |
|
|
892 |
/* how about inline-mmx-asm for this ... */
|
|
893 |
for (_i=2; _i<_sz; _i++) {
|
|
894 |
_prev = _this;
|
|
895 |
_this = _p[_i];
|
|
896 |
if (_prev < _min) {
|
|
897 |
_min = _prev;
|
|
898 |
} else if (_prev > _max) {
|
|
899 |
_max = _prev;
|
5737
|
900 |
}
|
|
901 |
}
|
7220
|
902 |
if (_this < _min) {
|
|
903 |
_min = _this;
|
|
904 |
} else if (_this > _max) {
|
|
905 |
_max = _this;
|
|
906 |
}
|
5737
|
907 |
}
|
7220
|
908 |
min = __MKFLOAT(_min);
|
|
909 |
max = __MKFLOAT(_max);
|
|
910 |
RETURN (__ARRAY_WITH2(min, max));
|
5737
|
911 |
}
|
7220
|
912 |
}
|
5737
|
913 |
%}.
|
7220
|
914 |
^ Array with:(super min) with:(super max)
|
5737
|
915 |
|
7220
|
916 |
"
|
|
917 |
|f1|
|
|
918 |
|
|
919 |
f1 := (1 to:1000) asFloatArray.
|
|
920 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 minMax ] ]
|
|
921 |
"
|
|
922 |
"
|
|
923 |
|f1|
|
5737
|
924 |
|
7220
|
925 |
f1 := FloatArray withAll:#(1 2 3 4 5).
|
|
926 |
f1 minMax
|
|
927 |
"
|
|
928 |
"
|
|
929 |
|f1|
|
5737
|
930 |
|
7220
|
931 |
f1 := FloatArray withAll:#(5 4 3 2 1).
|
|
932 |
f1 minMax
|
|
933 |
"
|
10561
|
934 |
!
|
|
935 |
|
17736
|
936 |
numFloats
|
|
937 |
^ self size
|
|
938 |
!
|
|
939 |
|
10561
|
940 |
squaredLength
|
17736
|
941 |
"Return the squared length of the receiver interpreted as vector"
|
11212
|
942 |
|
|
943 |
^ self dot: self
|
10561
|
944 |
! !
|
|
945 |
|
|
946 |
!FloatArray methodsFor:'vector arithmetic'!
|
|
947 |
|
|
948 |
dot: aFloatVector
|
|
949 |
"Return the dot product of the receiver and the argument.
|
|
950 |
Fail if the argument is not of the same size as the receiver."
|
|
951 |
|
|
952 |
| mySize result |
|
|
953 |
|
|
954 |
"/ <primitive:'primitiveDotProduct' module: 'FloatArrayPlugin'>
|
|
955 |
%{
|
11212
|
956 |
if (__isFloats(aFloatVector)
|
|
957 |
&& (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0))
|
|
958 |
&& (__ClassInstPtr(__qClass(aFloatVector))->c_ninstvars == __mkSmallInteger(0))) {
|
10561
|
959 |
int __mySize = __floatArraySize(self);
|
|
960 |
int __otherSize = __floatArraySize(aFloatVector);
|
|
961 |
if (__mySize == __otherSize) {
|
|
962 |
float *__p1 = __FloatArrayInstPtr(self)->f_element;
|
|
963 |
float *__p2 = __FloatArrayInstPtr(aFloatVector)->f_element;
|
|
964 |
float __result = 0.0;
|
|
965 |
int __i;
|
|
966 |
|
|
967 |
/* how about inline-mmx-asm for this ... */
|
|
968 |
for (__i=0; __i<__mySize; __i++) {
|
|
969 |
__result = __result + (__p1[__i] * __p2[__i]);
|
|
970 |
}
|
|
971 |
RETURN (__MKFLOAT(__result));
|
|
972 |
}
|
|
973 |
}
|
|
974 |
%}.
|
|
975 |
|
|
976 |
mySize := self size.
|
11212
|
977 |
mySize = aFloatVector size ifFalse:[^self error:'Must be of equal size'].
|
10561
|
978 |
result := 0.0.
|
|
979 |
1 to: mySize do:[:i|
|
|
980 |
result := result + ((self at: i) * (aFloatVector at: i)).
|
|
981 |
].
|
|
982 |
^result
|
|
983 |
|
|
984 |
"
|
|
985 |
|v|
|
|
986 |
|
11212
|
987 |
v := #(2.0 2.0 1.0) asFloatArray.
|
|
988 |
v dot:v.
|
10561
|
989 |
"
|
|
990 |
|
|
991 |
"Created: / 29-05-2007 / 13:13:39 / cg"
|
5737
|
992 |
! !
|
|
993 |
|
628
|
994 |
!FloatArray class methodsFor:'documentation'!
|
603
|
995 |
|
|
996 |
version
|
17846
|
997 |
^ '$Id: FloatArray.st 10660 2011-07-18 15:22:09Z vranyj1 $'
|
17736
|
998 |
!
|
|
999 |
|
|
1000 |
version_CVS
|
17845
|
1001 |
^ '§Header: /cvs/stx/stx/libbasic/FloatArray.st,v 1.26 2009/12/01 22:11:19 cg Exp §'
|
17846
|
1002 |
! !
|