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