|
Class: DoubleArray
Object
|
+--Collection
|
+--SequenceableCollection
|
+--ArrayedCollection
|
+--UninterpretedBytes
|
+--AbstractNumberVector
|
+--UnboxedFloatArray
|
+--DoubleArray
- Package:
- stx:libbasic
- Category:
- Collections-Arrayed
- Version:
- rev:
1.60
date: 2022/08/10 10:06:57
- user: cg
- file: DoubleArray.st directory: libbasic
- module: stx stc-classLibrary: libbasic
DoubleArrays store double precision float values (and nothing else).
They have been added to support heavy duty number crunching and
data exchange with openGL frameworks and other mass data libraries
somewhat better than other Smalltalks do.
Storing Floats & Doubles in these objects (instead of Arrays)
has some benefits:
1) since the values are stored directly (instead of pointers to them)
both access overhead and garbage collect overhead is minimized.
2) they can be much faster passed to c functions (such as graphics
libraries or heavy duty math packages), since the double values
come packed and can be used in C by using a (double *) or double[].
There is no need to loop over the array extracting doubles.
3) they could (in theory) be much more easily be processed by things like
vector and array processors
Be aware however, that Float- and DoubleArrays are not supported in other
Smalltalks - your program will thus become somewhat less portable.
(since their protocol is the same as normal arrays filled with floats,
they can of course be easily simulated - a bit slower though)
However, they could be simulated by a ByteArray, using doubleAt: and
doubleAtPut: messages to access the elements, but that seems a bit
clumsy and unelegant. Also, the stc-compiler may learn how to deal
with Float- and DoubleArrays, making accesses very fast in the future.
Hint: if you use doubleArrays in your application and must port it
to some other smalltalk, define a DoubleArray class there, which is derived
from ByteArray, and add access methods.
Of course, DoubleArray can be subclassed,
and named instance variables can be added there.
DoubleArrays can be used as literals i.e. you can enter DoubleArray-constants as:
#f64( element1 element2 .... elementN )
for example:
#f64(1 2.0 3 4.0)
See example uses in the GLX interface and GLDemos.
Aliased as Float64Array.
[memory requirements:]
OBJ-HEADER + (size * double-size)
[complexity:]
see Array
copyrightCOPYRIGHT (c) 1993 by Claus Gittinger
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
queries
-
elementByteSize
-
for bit-like containers, return the number of bytes stored per element.
Here, 8 is returned
-
epsilon
-
return the maximum relative spacing of my elements
(i.e. the value-delta of the least significant bit)
-
fmax
-
return the largest value that instances of me can represent
-
fmin
-
return the smallest normalized non-zero value that instances of me can hold
-
literalTokenPrefix
-
-
precision
-
answer the precision (the number of bits in the mantissa) of my elements (in bits)
This is an IEEE double, where only the fraction from the normalized mantissa is stored
and so there is a hidden bit and the mantissa is actually represented by 53 binary digits
(although only 52 are needed in the binary representation)
the hidden bit is included here
copying
-
clone
-
Return a clone-copy of the receiver
Usage example(s):
|f1|
f1 := DoubleArray withAll:#(1 2 3 4 5).
f1 clone
|
destructive arithmetic support
-
primAbs
-
destructive absolute value of each element;
does not check for immutability: only use internally after cloning
-
primAddArray: doubleArray
-
add the vector argument into the receiver (destructive).
The argument must be another vector of the same or larger size;
if the arguments size is larger, only the first n (=self size) elements are added in.
Does not check for immutability: only use internally after cloning
-
primAddScalar: aScalar
-
add the scalar argument into the receiver (destructive).
does not check for immutability: only use internally after cloning
-
primDivideArray: doubleArray
-
divide the vector argument into the receiver (destructive).
The argument must be another vector of the same or larger size;
if the arguments size is larger, only the first n (=self size) elements are added in.
Does not check for immutability: only use internally after cloning
-
primMulArray: doubleArray
-
multiply the vector argument into the receiver (destructive).
The argument must be another vector of the same or larger size;
if the arguments size is larger, only the first n (=self size) elements are added in.
Does not check for immutability: only use internally after cloning
-
primMulScalar: aScalar
-
multiply the scalar argument into the receiver (destructive).
does not check for immutability: only use internally after cloning
-
primNegated
-
destructive negative value of each element.
does not check for immutability: only use internally after cloning
-
primSubtractArray: doubleArray
-
subtract the vector argument into the receiver (destructive).
The argument must be another vector of the same or larger size;
if the arguments size is larger, only the first n (=self size) elements are added in.
Does not check for immutability: only use internally after cloning
filling & replacing
-
from: startIndex to: endIndex put: aValue
-
replace some elements of the collection by the argument, aNumber.
Return the receiver.
The argument, aValue must be a number which can be converted to a float.
Notice: This operation modifies the receiver, NOT a copy;
therefore the change may affect all others referencing the receiver.
Usage example(s):
|f|
f := DoubleArray new:100.
f from:2 to:5 put:1.
f from:90 to:99 put:1.
f from:1 to:100 put:2.
f
|
queries
-
defaultElement
-
-
max
-
return the largest element;
redefined for speed
Usage example(s):
|f1|
f1 := (1 to:10000) asDoubleArray.
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 max ] ]
|
Usage example(s):
|a1|
a1 := (1 to:10000) asArray collect:#asFloat.
Time millisecondsToRun:[ 1000 timesRepeat:[ a1 max ] ]
|
Usage example(s):
|f1|
f1 := DoubleArray withAll:#(1 2 3 4 5).
f1 max
|
Usage example(s):
|f1|
f1 := DoubleArray withAll:#(5 4 3 2 1).
f1 max
|
-
min
-
return the smallest element;
redefined for speed
Usage example(s):
|f1|
f1 := (1 to:10000) asDoubleArray.
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 min ] ]
|
Usage example(s):
|a1|
a1 := (1 to:10000) asArray collect:#asFloat.
Time millisecondsToRun:[ 1000 timesRepeat:[ a1 min ] ]
|
Usage example(s):
|f1|
f1 := DoubleArray withAll:#(1 2 3 4 5).
f1 min
|
Usage example(s):
|f1|
f1 := DoubleArray withAll:#(5 4 3 2 1).
f1 min
|
-
minMax
-
return a Tuple holding the smallest and largest element;
redefined for speed
Usage example(s):
|f1|
f1 := (1 to:10000) asDoubleArray.
Time millisecondsToRun:[ 1000 timesRepeat:[ f1 minMax ] ]
|
Usage example(s):
|f1|
f1 := DoubleArray withAll:#(1 2 3 4 5).
f1 minMax
|
Usage example(s):
|f1|
f1 := DoubleArray withAll:#(5 4 3 2 1).
f1 minMax
|
testing
-
isFloat64Array
-
return true if the receiver has float64 elements
vector arithmetic
-
dot: aFloatVector
-
Return the dot product of the receiver and the argument.
Raises an error, if the argument is not of the same size as the receiver.
Usage example(s):
|v|
v := #(2.0 2.0 1.0) asFloatArray.
v dot:v.
|
Usage example(s):
|v1 v2|
v1 := FloatArray new:10000 withAll:2.
v2 := FloatArray new:10000 withAll:3.
Time millisecondsToRun:[
10000 timesRepeat:[
v1 dot:v2.
]
]
|
-
esum
-
Return the sum of the elements.
Reduces accumulated rounding errors.
This is an experimental algorithm
Usage example(s):
here, sum and esum both deliver the same result
|v|
v := #(2.0 2.0 1.0 1.0) asDoubleArray.
v sum.
v esum.
|
Usage example(s):
here, sum looses and delivers a wrong result
(the first 1.0 is lost due to limited precision)
|v|
v := #(1e100 1.0 -1e100 1.0) asDoubleArray.
v sum.
v esum.
|
-
hornerMultiplyAndAdd: x
-
primitive support for horner's-method computation of polynomials.
The vector is interpreted as providing the factors for a polynomial,
an*x^n + (an-1)*x^(n-1) + ... + a2(x) + a1
where the ai are the elements of the Array.
The highest rank factor is at the first position, the 0-rank constant at last.
This is inlined c-code, which may get compiled to fast machine code,
using multiply-and-add or vector instructions, if the CPU/Compiler support them.
-
sum
-
Return the sum of the elements.
May suffer from accumulated rounding error
|