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