DoubleArray.st
changeset 19031 b8bbe1983d3a
parent 16853 e3e062d68503
child 19035 06aeb1259ba6
child 20301 af76baeacf78
equal deleted inserted replaced
19030:688b462a4941 19031:b8bbe1983d3a
     8  be provided or otherwise made available to, or used by, any
     8  be provided or otherwise made available to, or used by, any
     9  other person.  No title to or ownership of the software is
     9  other person.  No title to or ownership of the software is
    10  hereby transferred.
    10  hereby transferred.
    11 "
    11 "
    12 "{ Package: 'stx:libbasic' }"
    12 "{ Package: 'stx:libbasic' }"
       
    13 
       
    14 "{ NameSpace: Smalltalk }"
    13 
    15 
    14 AbstractNumberVector variableDoubleSubclass:#DoubleArray
    16 AbstractNumberVector variableDoubleSubclass:#DoubleArray
    15 	instanceVariableNames:''
    17 	instanceVariableNames:''
    16 	classVariableNames:''
    18 	classVariableNames:''
    17 	poolDictionaries:''
    19 	poolDictionaries:''
    81     [author:]
    83     [author:]
    82         Claus Gittinger
    84         Claus Gittinger
    83 "
    85 "
    84 ! !
    86 ! !
    85 
    87 
    86 
       
    87 
       
    88 !DoubleArray class methodsFor:'queries'!
    88 !DoubleArray class methodsFor:'queries'!
    89 
    89 
    90 elementByteSize
    90 elementByteSize
    91     "for bit-like containers, return the number of bytes stored per element.
    91     "for bit-like containers, return the number of bytes stored per element.
    92      Here, 8 is returned"
    92      Here, 8 is returned"
    94     ^ 8
    94     ^ 8
    95 
    95 
    96     "Created: / 15-09-2011 / 14:12:46 / cg"
    96     "Created: / 15-09-2011 / 14:12:46 / cg"
    97 ! !
    97 ! !
    98 
    98 
    99 
       
   100 
       
   101 !DoubleArray methodsFor:'queries'!
    99 !DoubleArray methodsFor:'queries'!
   102 
   100 
   103 defaultElement
   101 defaultElement
   104     ^ Float zero
   102     ^ Float zero
   105 ! !
   103 ! !
   106 
   104 
       
   105 !DoubleArray methodsFor:'vector arithmetic'!
       
   106 
       
   107 dot: aFloatVector
       
   108     "Return the dot product of the receiver and the argument.
       
   109      Raises an error, if the argument is not of the same size as the receiver."
       
   110 
       
   111     | mySize result |
       
   112 
       
   113 %{
       
   114     if ((__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0))
       
   115      && (__ClassInstPtr(__qClass(aFloatVector))->c_ninstvars == __mkSmallInteger(0))) {
       
   116         INT __mySize = __doubleArraySize(self);
       
   117         double __result = 0.0;
       
   118         double *__p1 = __DoubleArrayInstPtr(self)->d_element;
       
   119 
       
   120         if (__mySize > 0) {
       
   121             if (__isFloats(aFloatVector)) {
       
   122                 INT __otherSize = __floatArraySize(aFloatVector);
       
   123 
       
   124                 if (__mySize == __otherSize) {
       
   125                     float *__p2 = __FloatArrayInstPtr(aFloatVector)->f_element;
       
   126                     INT __i;
       
   127                     /* how about inline-mmx-asm for this ... */
       
   128                     for (__i=0; __i<__mySize; __i++) {
       
   129                         __result += (__p1[__i] * __p2[__i]);
       
   130                     }
       
   131                     RETURN (__MKFLOAT(__result));
       
   132                 }
       
   133             } else if (__isDoubles(aFloatVector)) {
       
   134                 INT __otherSize = __doubleArraySize(aFloatVector);
       
   135 
       
   136                 if (__mySize == __otherSize) {
       
   137                     double *__p2 = __DoubleArrayInstPtr(aFloatVector)->d_element;
       
   138                     INT __i;
       
   139                     /* how about inline-mmx-asm for this ... */
       
   140                     for (__i=0; __i<__mySize; __i++) {
       
   141                         __result += (__p1[__i] * __p2[__i]);
       
   142                     }
       
   143                     RETURN (__MKFLOAT(__result));
       
   144                 }
       
   145             }
       
   146         }
       
   147     }
       
   148 %}.
       
   149     ^ super dot:aFloatVector
       
   150 
       
   151     "
       
   152      |v|
       
   153 
       
   154      v := #(2.0 2.0 1.0) asFloatArray.
       
   155      v dot:v.
       
   156     "
       
   157     "
       
   158      |v1 v2|
       
   159 
       
   160      v1 := FloatArray new:10000 withAll:2.
       
   161      v2 := FloatArray new:10000 withAll:3.
       
   162      Time millisecondsToRun:[
       
   163         10000 timesRepeat:[
       
   164           v1 dot:v2.
       
   165         ]
       
   166      ]
       
   167     "
       
   168 
       
   169     "Created: / 29-05-2007 / 13:13:39 / cg"
       
   170 !
       
   171 
       
   172 hornerMultiplyAndAdd:x
       
   173     "primitive support for horner's-method computation of polynomials.
       
   174      The vector is interpreted as providing the factors for a polynomial,
       
   175         an*x^n + (an-1)*x^(n-1) + ... + a2(x) + a1
       
   176      where the ai are the elements of the Array.
       
   177      The highest rank factor is at the first position, the 0-rank constant at last.
       
   178      This is inlined c-code, which may get compiled to fast machine code,
       
   179      using multiply-and-add or vector instructions, if the CPU/Compiler support them."
       
   180 
       
   181     | mySize result |
       
   182 
       
   183 %{
       
   184     double __x;
       
   185 
       
   186     if (__isFloat(x)) {
       
   187         __x = __floatVal(x);
       
   188     } else if (__isShortFloat(x)) {
       
   189         __x = (double)__shortFloatVal(x);
       
   190     } else if (__isSmallInteger(x)) {
       
   191         __x = (double)(__intVal(x));
       
   192     } else {
       
   193         goto getOutOfHere;
       
   194     }
       
   195 
       
   196     if (__ClassInstPtr(__qClass(self))->c_ninstvars == __mkSmallInteger(0)) {
       
   197         INT __mySize = __doubleArraySize(self);
       
   198         double *__elements = __DoubleArrayInstPtr(self)->d_element;
       
   199         double __result = __elements[0];
       
   200 
       
   201         if (__mySize > 1) {
       
   202             INT __i;
       
   203             /* how about inline-mmx-asm for this ... */
       
   204             for (__i=1; __i<__mySize; __i++) {
       
   205                 __result = (__result * __x) + __elements[__i];
       
   206             }
       
   207         }
       
   208         RETURN (__MKFLOAT(__result));
       
   209     }
       
   210 getOutOfHere: ;    
       
   211 %}.
       
   212     ^ super hornerMultiplyAndAdd:x
       
   213 
       
   214     "
       
   215      |v|
       
   216      v := #(2.0 3.0 4.0) asDoubleArray.
       
   217      v  hornerMultiplyAndAdd:10.
       
   218     
       
   219      |v|
       
   220      v := Array new:100 withAll:2.0.
       
   221      v hornerMultiplyAndAdd:10
       
   222 
       
   223      |v|
       
   224      v := Array new:100 withAll:2.0.
       
   225      Time millisecondsToRun:[
       
   226         10000 timesRepeat:[ v hornerMultiplyAndAdd:10]
       
   227      ]
       
   228 
       
   229      |v|
       
   230      v := FloatArray new:100 withAll:2.
       
   231      Time millisecondsToRun:[
       
   232         10000 timesRepeat:[ v hornerMultiplyAndAdd:10]
       
   233      ]
       
   234 
       
   235      |v|
       
   236      v := DoubleArray new:100 withAll:2.
       
   237      Time millisecondsToRun:[
       
   238         10000 timesRepeat:[ v hornerMultiplyAndAdd:10]
       
   239      ]
       
   240     "
       
   241 ! !
       
   242 
   107 !DoubleArray class methodsFor:'documentation'!
   243 !DoubleArray class methodsFor:'documentation'!
   108 
   244 
   109 version
   245 version
   110     ^ '$Header: /cvs/stx/stx/libbasic/DoubleArray.st,v 1.24 2014-09-23 20:24:21 cg Exp $'
   246     ^ '$Header$'
   111 !
   247 !
   112 
   248 
   113 version_CVS
   249 version_CVS
   114     ^ '$Header: /cvs/stx/stx/libbasic/DoubleArray.st,v 1.24 2014-09-23 20:24:21 cg Exp $'
   250     ^ '$Header$'
   115 ! !
   251 ! !
   116 
   252