author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 26 Apr 2010 19:26:38 +0100 | |
branch | jv |
changeset 17761 | b0e5971141bc |
parent 17736 | 26cfea63471d |
child 17767 | a4a32df3aa5e |
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 |
||
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 |
||
702 |
!FloatArray methodsFor:'queries'! |
|
703 |
||
5768 | 704 |
absMax |
705 |
"return the largest absolute value" |
|
706 |
||
5816 | 707 |
|mm| |
5768 | 708 |
|
709 |
mm := self minMax. |
|
710 |
^ (mm at:1) abs max:(mm at:2) abs |
|
711 |
||
712 |
" |
|
713 |
|f1| |
|
714 |
||
715 |
f1 := (1 to:1000) asFloatArray. |
|
716 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 absMax ] ] |
|
717 |
" |
|
11212 | 718 |
|
5768 | 719 |
" |
720 |
|f1| |
|
721 |
||
722 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
|
723 |
f1 absMax |
|
724 |
" |
|
725 |
" |
|
726 |
|f1| |
|
727 |
||
728 |
f1 := FloatArray withAll:#(5 4 3 2 1). |
|
729 |
f1 absMax |
|
730 |
" |
|
731 |
" |
|
732 |
|f1| |
|
733 |
||
734 |
f1 := FloatArray withAll:#(5 -4 3 2 1). |
|
735 |
f1 absMax |
|
736 |
" |
|
737 |
" |
|
738 |
|f1| |
|
739 |
||
740 |
f1 := FloatArray withAll:#(5 -5 3 2 1). |
|
741 |
f1 absMax |
|
742 |
" |
|
743 |
" |
|
744 |
|f1| |
|
745 |
||
746 |
f1 := FloatArray withAll:#(5 -6 3 2 1). |
|
747 |
f1 absMax |
|
748 |
" |
|
749 |
! |
|
750 |
||
7220 | 751 |
defaultElement |
752 |
^ ShortFloat zero |
|
753 |
! |
|
754 |
||
10561 | 755 |
length |
17736 | 756 |
"Return the length of the receiver interpreted as vector |
757 |
(that is the length of the vector from 0.0 @ 0.0 @ ... @ 0.0 |
|
11212 | 758 |
to the point in the n-dimensional space represented by the receiver)" |
759 |
||
760 |
^ self squaredLength sqrt |
|
761 |
||
762 |
" |
|
763 |
#(10.0 10.0) asFloatArray length |
|
764 |
#(10.0 10.0 10.0) asFloatArray length |
|
765 |
" |
|
10561 | 766 |
! |
767 |
||
5737 | 768 |
max |
769 |
"return the largest element; |
|
770 |
redefined for speed" |
|
771 |
%{ |
|
8913 | 772 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
7220 | 773 |
int _sz = __floatArraySize(self); |
5737 | 774 |
|
7220 | 775 |
if (_sz > 0) { |
776 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
777 |
float _max; |
|
5737 | 778 |
|
7220 | 779 |
_max = _p[0]; |
780 |
if (_sz > 1) { |
|
781 |
int _i; |
|
782 |
float _prev, _this; |
|
5737 | 783 |
|
7220 | 784 |
/* how about inline-mmx-asm for this ... */ |
785 |
_this = _p[1]; |
|
786 |
for (_i=2; _i<_sz; _i++) { |
|
787 |
_prev = _this; |
|
788 |
_this = _p[_i]; |
|
789 |
if (_prev > _max) _max = _prev; |
|
5737 | 790 |
} |
7220 | 791 |
if (_this > _max) _max = _this; |
5737 | 792 |
} |
7220 | 793 |
RETURN (__MKFLOAT(_max)); |
5737 | 794 |
} |
7220 | 795 |
} |
5737 | 796 |
%}. |
7220 | 797 |
^ super max |
5737 | 798 |
|
7220 | 799 |
" |
800 |
|f1| |
|
5737 | 801 |
|
7220 | 802 |
f1 := (1 to:1000) asFloatArray. |
803 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 max ] ] |
|
804 |
" |
|
805 |
" |
|
806 |
|f1| |
|
5737 | 807 |
|
7220 | 808 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
809 |
f1 max |
|
810 |
" |
|
811 |
" |
|
812 |
|f1| |
|
5737 | 813 |
|
7220 | 814 |
f1 := FloatArray withAll:#(5 4 3 2 1). |
815 |
f1 max |
|
816 |
" |
|
5737 | 817 |
! |
818 |
||
819 |
min |
|
820 |
"return the largest element; |
|
821 |
redefined for speed" |
|
822 |
%{ |
|
8913 | 823 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
7220 | 824 |
int _sz = __floatArraySize(self); |
5737 | 825 |
|
7220 | 826 |
if (_sz > 0) { |
827 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
828 |
float _min; |
|
5737 | 829 |
|
7220 | 830 |
_min = _p[0]; |
831 |
if (_sz > 1) { |
|
832 |
int _i; |
|
833 |
float _prev, _this; |
|
5737 | 834 |
|
7220 | 835 |
/* how about inline-mmx-asm for this ... */ |
836 |
_this = _p[1]; |
|
837 |
for (_i=2; _i<_sz; _i++) { |
|
838 |
_prev = _this; |
|
839 |
_this = _p[_i]; |
|
840 |
if (_prev < _min) _min = _prev; |
|
5737 | 841 |
} |
7220 | 842 |
if (_this < _min) _min = _this; |
5737 | 843 |
} |
7220 | 844 |
RETURN (__MKFLOAT(_min)); |
5737 | 845 |
} |
7220 | 846 |
} |
5737 | 847 |
%}. |
7220 | 848 |
^ super min |
5737 | 849 |
|
7220 | 850 |
" |
851 |
|f1| |
|
5737 | 852 |
|
7220 | 853 |
f1 := (1 to:1000) asFloatArray. |
854 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 min ] ] |
|
855 |
" |
|
856 |
" |
|
857 |
|f1| |
|
5737 | 858 |
|
7220 | 859 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
860 |
f1 min |
|
861 |
" |
|
862 |
" |
|
863 |
|f1| |
|
5737 | 864 |
|
7220 | 865 |
f1 := FloatArray withAll:#(5 4 3 2 1). |
866 |
f1 min |
|
867 |
" |
|
5737 | 868 |
! |
869 |
||
870 |
minMax |
|
11212 | 871 |
"return a Tuple holding the smallest and largest element; |
5737 | 872 |
redefined for speed" |
873 |
||
874 |
|min max| |
|
875 |
||
876 |
%{ |
|
8913 | 877 |
if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) { |
7220 | 878 |
int _sz = __floatArraySize(self); |
879 |
if (_sz > 0) { |
|
880 |
int _i; |
|
881 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
882 |
float _min, _max; |
|
5737 | 883 |
|
7220 | 884 |
_min = _max = _p[0]; |
5737 | 885 |
|
7220 | 886 |
if (_sz > 1) { |
887 |
float _this = _p[1]; |
|
888 |
float _prev; |
|
889 |
||
890 |
/* how about inline-mmx-asm for this ... */ |
|
891 |
for (_i=2; _i<_sz; _i++) { |
|
892 |
_prev = _this; |
|
893 |
_this = _p[_i]; |
|
894 |
if (_prev < _min) { |
|
895 |
_min = _prev; |
|
896 |
} else if (_prev > _max) { |
|
897 |
_max = _prev; |
|
5737 | 898 |
} |
899 |
} |
|
7220 | 900 |
if (_this < _min) { |
901 |
_min = _this; |
|
902 |
} else if (_this > _max) { |
|
903 |
_max = _this; |
|
904 |
} |
|
5737 | 905 |
} |
7220 | 906 |
min = __MKFLOAT(_min); |
907 |
max = __MKFLOAT(_max); |
|
908 |
RETURN (__ARRAY_WITH2(min, max)); |
|
5737 | 909 |
} |
7220 | 910 |
} |
5737 | 911 |
%}. |
7220 | 912 |
^ Array with:(super min) with:(super max) |
5737 | 913 |
|
7220 | 914 |
" |
915 |
|f1| |
|
916 |
||
917 |
f1 := (1 to:1000) asFloatArray. |
|
918 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 minMax ] ] |
|
919 |
" |
|
920 |
" |
|
921 |
|f1| |
|
5737 | 922 |
|
7220 | 923 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
924 |
f1 minMax |
|
925 |
" |
|
926 |
" |
|
927 |
|f1| |
|
5737 | 928 |
|
7220 | 929 |
f1 := FloatArray withAll:#(5 4 3 2 1). |
930 |
f1 minMax |
|
931 |
" |
|
10561 | 932 |
! |
933 |
||
17736 | 934 |
numFloats |
935 |
^ self size |
|
936 |
! |
|
937 |
||
10561 | 938 |
squaredLength |
17736 | 939 |
"Return the squared length of the receiver interpreted as vector" |
11212 | 940 |
|
941 |
^ self dot: self |
|
10561 | 942 |
! ! |
943 |
||
944 |
!FloatArray methodsFor:'vector arithmetic'! |
|
945 |
||
946 |
dot: aFloatVector |
|
947 |
"Return the dot product of the receiver and the argument. |
|
948 |
Fail if the argument is not of the same size as the receiver." |
|
949 |
||
950 |
| mySize result | |
|
951 |
||
952 |
"/ <primitive:'primitiveDotProduct' module: 'FloatArrayPlugin'> |
|
953 |
%{ |
|
11212 | 954 |
if (__isFloats(aFloatVector) |
955 |
&& (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) |
|
956 |
&& (__ClassInstPtr(__qClass(aFloatVector))->c_ninstvars == __mkSmallInteger(0))) { |
|
10561 | 957 |
int __mySize = __floatArraySize(self); |
958 |
int __otherSize = __floatArraySize(aFloatVector); |
|
959 |
if (__mySize == __otherSize) { |
|
960 |
float *__p1 = __FloatArrayInstPtr(self)->f_element; |
|
961 |
float *__p2 = __FloatArrayInstPtr(aFloatVector)->f_element; |
|
962 |
float __result = 0.0; |
|
963 |
int __i; |
|
964 |
||
965 |
/* how about inline-mmx-asm for this ... */ |
|
966 |
for (__i=0; __i<__mySize; __i++) { |
|
967 |
__result = __result + (__p1[__i] * __p2[__i]); |
|
968 |
} |
|
969 |
RETURN (__MKFLOAT(__result)); |
|
970 |
} |
|
971 |
} |
|
972 |
%}. |
|
973 |
||
974 |
mySize := self size. |
|
11212 | 975 |
mySize = aFloatVector size ifFalse:[^self error:'Must be of equal size']. |
10561 | 976 |
result := 0.0. |
977 |
1 to: mySize do:[:i| |
|
978 |
result := result + ((self at: i) * (aFloatVector at: i)). |
|
979 |
]. |
|
980 |
^result |
|
981 |
||
982 |
" |
|
983 |
|v| |
|
984 |
||
11212 | 985 |
v := #(2.0 2.0 1.0) asFloatArray. |
986 |
v dot:v. |
|
10561 | 987 |
" |
988 |
||
989 |
"Created: / 29-05-2007 / 13:13:39 / cg" |
|
5737 | 990 |
! ! |
991 |
||
628 | 992 |
!FloatArray class methodsFor:'documentation'! |
603 | 993 |
|
994 |
version |
|
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17736
diff
changeset
|
995 |
^ '$Id: FloatArray.st 10517 2010-04-26 18:26:38Z vranyj1 $' |
17736 | 996 |
! |
997 |
||
998 |
version_CVS |
|
999 |
^ '§Header: /cvs/stx/stx/libbasic/FloatArray.st,v 1.26 2009/12/01 22:11:19 cg Exp §' |
|
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17736
diff
changeset
|
1000 |
! |
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17736
diff
changeset
|
1001 |
|
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17736
diff
changeset
|
1002 |
version_SVN |
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17736
diff
changeset
|
1003 |
^ '$Id: FloatArray.st 10517 2010-04-26 18:26:38Z vranyj1 $' |
88 | 1004 |
! ! |