author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 28 Jan 2013 21:53:19 +0000 | |
branch | jv |
changeset 18017 | 7fef9e17913f |
parent 18011 | deb0c3355881 |
parent 14692 | 98e7a51e5d17 |
child 18120 | e3a375d5f6a8 |
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:] |
14692 | 43 |
OBJ-HEADER + (size * float-size) |
4062 | 44 |
|
1263 | 45 |
[See also:] |
14692 | 46 |
DoubleArray Array |
1290 | 47 |
|
48 |
[author:] |
|
14692 | 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 |
" |
|
14692 | 73 |
#(1 2 3 4) asFloatArray * 3 |
5737 | 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 |
" |
|
14692 | 85 |
#(1 2 3 4) asFloatArray + 3 |
86 |
#(1 2 3 4) asFloatArray + #(1 2 3 4) asFloatArray |
|
5737 | 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 |
" |
|
14692 | 97 |
#(1 2 3 4) asFloatArray - 3 |
98 |
#(1 2 3 4) asFloatArray - #(1 2 3 4) asFloatArray |
|
10578 | 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 |
" |
|
14692 | 111 |
#(1 2 3 4) asFloatArray / 3 |
112 |
#(1 2 3 4) asFloatArray / #(1 2 3 4) asFloatArray |
|
10579 | 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)) { |
|
14692 | 135 |
INT _sz = __floatArraySize(self); |
136 |
INT i; |
|
137 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
10580 | 138 |
|
14692 | 139 |
/* how about inline-mmx-asm for this ... */ |
140 |
for (i=0; i<_sz; i++) { |
|
141 |
float f = _p[i]; |
|
10580 | 142 |
|
14692 | 143 |
if (f < 0) { |
144 |
_p[i] = -f; |
|
145 |
} |
|
146 |
} |
|
147 |
RETURN (self); |
|
10580 | 148 |
} |
14692 | 149 |
%}. |
10580 | 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). |
|
14692 | 156 |
f abs. |
10580 | 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))) { |
14692 | 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; |
|
5737 | 176 |
|
14692 | 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); |
|
5737 | 184 |
} |
14692 | 185 |
%}. |
5737 | 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. |
|
14692 | 194 |
f1 |
5737 | 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)) { |
14692 | 203 |
INT _sz1 = __floatArraySize(self); |
204 |
INT i; |
|
205 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
206 |
float v; |
|
5737 | 207 |
|
14692 | 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; |
|
5737 | 216 |
|
14692 | 217 |
/* how about inline-mmx-asm for this ... */ |
218 |
for (i=0; i<_sz1; i++) { |
|
219 |
_p1[i] += v; |
|
220 |
} |
|
221 |
RETURN (self); |
|
5737 | 222 |
} |
223 |
badArg: ; |
|
14692 | 224 |
%}. |
5737 | 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. |
|
14692 | 232 |
Transcript showCR:f1. |
5737 | 233 |
f1 += 2.0 asShortFloat. |
14692 | 234 |
Transcript showCR:f1. |
5737 | 235 |
f1 += 2. |
14692 | 236 |
Transcript showCR:f1. |
5737 | 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))) { |
|
14692 | 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; |
|
10561 | 253 |
|
14692 | 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); |
|
10561 | 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. |
|
14692 | 271 |
f1 |
10561 | 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)) { |
|
14692 | 282 |
INT _sz1 = __floatArraySize(self); |
283 |
INT i; |
|
284 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
285 |
float v; |
|
10561 | 286 |
|
14692 | 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; |
|
10561 | 295 |
|
14692 | 296 |
/* how about inline-mmx-asm for this ... */ |
297 |
for (i=0; i<_sz1; i++) { |
|
298 |
_p1[i] /= v; |
|
299 |
} |
|
300 |
RETURN (self); |
|
10561 | 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. |
|
14692 | 311 |
Transcript showCR:f1. |
10561 | 312 |
f1 /= 2.0 asShortFloat. |
14692 | 313 |
Transcript showCR:f1. |
10561 | 314 |
f1 /= 2. |
14692 | 315 |
Transcript showCR:f1. |
10561 | 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))) { |
14692 | 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; |
|
5737 | 334 |
|
14692 | 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); |
|
5737 | 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. |
|
14692 | 352 |
f1 |
5737 | 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)) { |
14692 | 361 |
INT _sz1 = __floatArraySize(self); |
362 |
INT i; |
|
363 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
364 |
float v; |
|
5737 | 365 |
|
14692 | 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; |
|
5737 | 374 |
|
14692 | 375 |
/* how about inline-mmx-asm for this ... */ |
376 |
for (i=0; i<_sz1; i++) { |
|
377 |
_p1[i] *= v; |
|
378 |
} |
|
379 |
RETURN (self); |
|
5737 | 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. |
|
14692 | 390 |
Transcript showCR:f1. |
5737 | 391 |
f1 *= 2.0 asShortFloat. |
14692 | 392 |
Transcript showCR:f1. |
5737 | 393 |
f1 *= 2. |
14692 | 394 |
Transcript showCR:f1. |
5737 | 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)) { |
|
14692 | 403 |
INT _sz = __floatArraySize(self); |
404 |
INT i; |
|
405 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
10580 | 406 |
|
14692 | 407 |
/* how about inline-mmx-asm for this ... */ |
408 |
for (i=0; i<_sz; i++) { |
|
409 |
float f = _p[i]; |
|
10580 | 410 |
|
14692 | 411 |
_p[i] = -f; |
412 |
} |
|
413 |
RETURN (self); |
|
10580 | 414 |
} |
14692 | 415 |
%}. |
10580 | 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). |
|
14692 | 422 |
f negated. |
423 |
f |
|
10580 | 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))) { |
|
14692 | 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; |
|
10578 | 442 |
|
14692 | 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); |
|
10578 | 450 |
} |
14692 | 451 |
%}. |
10578 | 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. |
|
14692 | 460 |
f1 |
10578 | 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)) { |
|
14692 | 471 |
INT _sz1 = __floatArraySize(self); |
472 |
INT i; |
|
473 |
float *_p1 = __FloatArrayInstPtr(self)->f_element; |
|
474 |
float v; |
|
10578 | 475 |
|
14692 | 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; |
|
10578 | 484 |
|
14692 | 485 |
/* how about inline-mmx-asm for this ... */ |
486 |
for (i=0; i<_sz1; i++) { |
|
487 |
_p1[i] -= v; |
|
488 |
} |
|
489 |
RETURN (self); |
|
10578 | 490 |
} |
491 |
badArg: ; |
|
14692 | 492 |
%}. |
10578 | 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. |
|
14692 | 500 |
Transcript showCR:f1. |
10578 | 501 |
f1 -= 2.0 asShortFloat. |
14692 | 502 |
Transcript showCR:f1. |
10578 | 503 |
f1 -= 2. |
14692 | 504 |
Transcript showCR:f1. |
10578 | 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 |
|
14692 | 517 |
ifTrue:[self primMulScalar: anObject asFloat] |
518 |
ifFalse:[self primMulArray: anObject] |
|
12577 | 519 |
|
520 |
" |
|
521 |
|f| |
|
522 |
||
523 |
f := #(1 2 3 4) asFloatArray. |
|
524 |
f *= 3. |
|
14692 | 525 |
f |
12577 | 526 |
" |
527 |
" |
|
528 |
|f| |
|
529 |
||
530 |
f := #(1 2 3 4) asFloatArray. |
|
531 |
f *= #(1 2 3 4) asFloatArray. |
|
14692 | 532 |
f |
12577 | 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 |
|
14692 | 541 |
ifTrue:[self primAddScalar: anObject asFloat] |
542 |
ifFalse:[self primAddArray: anObject] |
|
12577 | 543 |
|
544 |
" |
|
545 |
|f| |
|
546 |
||
547 |
f := #(1 2 3 4) asFloatArray. |
|
548 |
f += 3. |
|
14692 | 549 |
f |
12577 | 550 |
" |
551 |
" |
|
552 |
|f| |
|
553 |
||
554 |
f := #(1 2 3 4) asFloatArray. |
|
555 |
f += #(1 2 3 4) asFloatArray. |
|
14692 | 556 |
f |
12577 | 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 |
|
14692 | 565 |
ifTrue:[self primSubtractScalar: anObject asFloat] |
566 |
ifFalse:[self primSubtractArray: anObject] |
|
12577 | 567 |
|
568 |
" |
|
569 |
|f| |
|
570 |
||
571 |
f := #(1 2 3 4) asFloatArray. |
|
572 |
f -= 3. |
|
14692 | 573 |
f |
12577 | 574 |
" |
575 |
" |
|
576 |
|f| |
|
577 |
||
578 |
f := #(1 2 3 4) asFloatArray. |
|
579 |
f += #(1 2 3 4) asFloatArray. |
|
14692 | 580 |
f |
12577 | 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 |
|
14692 | 591 |
ifTrue:[self primDivScalar: anObject asFloat] |
592 |
ifFalse:[self primDivArray: anObject] |
|
12577 | 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)) { |
14692 | 607 |
INT _sz = __floatArraySize(self); |
5737 | 608 |
|
14692 | 609 |
bcopy(__FloatArrayInstPtr(self)->f_element, |
610 |
__FloatArrayInstPtr(newArr)->f_element, |
|
611 |
sizeof(float) * _sz); |
|
5737 | 612 |
|
14692 | 613 |
RETURN (newArr); |
5737 | 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). |
|
14692 | 623 |
f1 clone |
5737 | 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)) { |
14692 | 632 |
if (__bothSmallInteger(start, stop)) { |
633 |
INT __start = __intVal(start) - 1; |
|
634 |
INT __stop = __intVal(stop) - 1 ; |
|
5737 | 635 |
|
14692 | 636 |
if (__stop >= __start) { |
637 |
INT __sz = __floatArraySize(self); |
|
5737 | 638 |
|
14692 | 639 |
if (((unsigned INT)__start < __sz) |
640 |
&& ((unsigned INT)__stop < __sz)) { |
|
641 |
INT __n = __stop - __start + 1; |
|
642 |
OBJ __nObj; |
|
5737 | 643 |
|
14692 | 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 |
} |
|
5737 | 656 |
} |
14692 | 657 |
%}. |
5737 | 658 |
^ super copyFrom:start to:stop |
659 |
||
660 |
" |
|
661 |
|f1 f2| |
|
662 |
||
663 |
f1 := FloatArray withAll:#(1 2 3 4 5 6). |
|
14692 | 664 |
f2 := f1 copyFrom:2 to:4. |
665 |
f2 |
|
5737 | 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))) { |
14692 | 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 ; |
|
5737 | 679 |
|
14692 | 680 |
if (__stop >= __start) { |
681 |
INT __sz = __floatArraySize(self); |
|
682 |
INT __otherSz = __floatArraySize(aCollection); |
|
683 |
INT __replStop = __replStart + (__stop-__start); |
|
5737 | 684 |
|
14692 | 685 |
if (((unsigned INT)__start < __sz) |
686 |
&& ((unsigned INT)__stop < __sz) |
|
687 |
&& ((unsigned INT)__replStart < __otherSz) |
|
688 |
&& ((unsigned INT)__replStop < __otherSz)) { |
|
689 |
INT __n = __stop - __start + 1; |
|
5737 | 690 |
|
14692 | 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 |
} |
|
697 |
} |
|
698 |
} |
|
699 |
} |
|
5737 | 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. |
|
14692 | 709 |
f1 replaceFrom:1 to:3 with:f2 startingAt:3 |
5737 | 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). |
|
14692 | 734 |
f1 absMax |
5768 | 735 |
" |
736 |
" |
|
737 |
|f1| |
|
738 |
||
739 |
f1 := FloatArray withAll:#(5 4 3 2 1). |
|
14692 | 740 |
f1 absMax |
5768 | 741 |
" |
742 |
" |
|
743 |
|f1| |
|
744 |
||
745 |
f1 := FloatArray withAll:#(5 -4 3 2 1). |
|
14692 | 746 |
f1 absMax |
5768 | 747 |
" |
748 |
" |
|
749 |
|f1| |
|
750 |
||
751 |
f1 := FloatArray withAll:#(5 -5 3 2 1). |
|
14692 | 752 |
f1 absMax |
5768 | 753 |
" |
754 |
" |
|
755 |
|f1| |
|
756 |
||
757 |
f1 := FloatArray withAll:#(5 -6 3 2 1). |
|
14692 | 758 |
f1 absMax |
5768 | 759 |
" |
760 |
! |
|
761 |
||
7220 | 762 |
defaultElement |
763 |
^ ShortFloat zero |
|
764 |
! |
|
765 |
||
10561 | 766 |
length |
14692 | 767 |
"Return the length of the receiver interpreted as vector |
12577 | 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 |
" |
|
14692 | 774 |
#(10.0 10.0) asFloatArray length |
775 |
#(10.0 10.0 10.0) asFloatArray length |
|
11212 | 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)) { |
14692 | 784 |
INT _sz = __floatArraySize(self); |
5737 | 785 |
|
14692 | 786 |
if (_sz > 0) { |
787 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
788 |
float _max; |
|
5737 | 789 |
|
14692 | 790 |
_max = _p[0]; |
791 |
if (_sz > 1) { |
|
792 |
INT _i; |
|
793 |
float _prev, _this; |
|
5737 | 794 |
|
14692 | 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; |
|
801 |
} |
|
802 |
if (_this > _max) _max = _this; |
|
803 |
} |
|
804 |
RETURN (__MKFLOAT(_max)); |
|
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). |
14692 | 820 |
f1 max |
7220 | 821 |
" |
822 |
" |
|
823 |
|f1| |
|
5737 | 824 |
|
7220 | 825 |
f1 := FloatArray withAll:#(5 4 3 2 1). |
14692 | 826 |
f1 max |
7220 | 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)) { |
14692 | 835 |
INT _sz = __floatArraySize(self); |
5737 | 836 |
|
14692 | 837 |
if (_sz > 0) { |
838 |
float *_p = __FloatArrayInstPtr(self)->f_element; |
|
839 |
float _min; |
|
5737 | 840 |
|
14692 | 841 |
_min = _p[0]; |
842 |
if (_sz > 1) { |
|
843 |
INT _i; |
|
844 |
float _prev, _this; |
|
5737 | 845 |
|
14692 | 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; |
|
852 |
} |
|
853 |
if (_this < _min) _min = _this; |
|
854 |
} |
|
855 |
RETURN (__MKFLOAT(_min)); |
|
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). |
14692 | 871 |
f1 min |
7220 | 872 |
" |
873 |
" |
|
874 |
|f1| |
|
5737 | 875 |
|
7220 | 876 |
f1 := FloatArray withAll:#(5 4 3 2 1). |
14692 | 877 |
f1 min |
7220 | 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)) { |
14692 | 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 |
|
14692 | 895 |
_min = _max = _p[0]; |
13774 | 896 |
#ifdef PRE_4_OCT_2011 |
14692 | 897 |
if (_sz > 1) { |
898 |
float _this = _p[1]; |
|
899 |
float _prev; |
|
7220 | 900 |
|
14692 | 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; |
|
909 |
} |
|
910 |
} |
|
911 |
if (_this < _min) { |
|
912 |
_min = _this; |
|
913 |
} else if (_this > _max) { |
|
914 |
_max = _this; |
|
915 |
} |
|
916 |
} |
|
13774 | 917 |
#else |
14692 | 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 |
} |
|
13774 | 929 |
#endif |
14692 | 930 |
min = __MKFLOAT(_min); |
931 |
max = __MKFLOAT(_max); |
|
932 |
RETURN (__ARRAY_WITH2(min, max)); |
|
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. |
14692 | 942 |
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 minMax ] ] |
7220 | 943 |
" |
944 |
" |
|
945 |
|f1| |
|
5737 | 946 |
|
7220 | 947 |
f1 := FloatArray withAll:#(1 2 3 4 5). |
14692 | 948 |
f1 minMax |
7220 | 949 |
" |
950 |
" |
|
951 |
|f1| |
|
5737 | 952 |
|
7220 | 953 |
f1 := FloatArray withAll:#(5 4 3 2 1). |
14692 | 954 |
f1 minMax |
7220 | 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))) { |
|
14692 | 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; |
|
10561 | 990 |
|
14692 | 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 |
} |
|
10561 | 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| |
|
14692 | 1004 |
result := result + ((self at: i) * (aFloatVector at: i)). |
10561 | 1005 |
]. |
1006 |
^result |
|
1007 |
||
1008 |
" |
|
1009 |
|v| |
|
1010 |
||
11212 | 1011 |
v := #(2.0 2.0 1.0) asFloatArray. |
14692 | 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 |
|
14692 | 1021 |
^ '$Header: /cvs/stx/stx/libbasic/FloatArray.st,v 1.31 2013-01-23 18:03:01 cg Exp $' |
12577 | 1022 |
! |
1023 |
||
1024 |
version_CVS |
|
14692 | 1025 |
^ '$Header: /cvs/stx/stx/libbasic/FloatArray.st,v 1.31 2013-01-23 18:03:01 cg Exp $' |
88 | 1026 |
! ! |