AbstractNumberVector.st
author Claus Gittinger <cg@exept.de>
Tue, 04 Feb 2014 17:40:25 +0100
changeset 15934 2ac38e84aab5
parent 14026 d72d69757a61
child 17163 506b7f2ef014
permissions -rw-r--r--
class: AbstractNumberVector added: #absMax #dot: #length #squaredLength

"
 COPYRIGHT (c) 2011 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.
"
"{ Package: 'stx:libbasic' }"

UninterpretedBytes subclass:#AbstractNumberVector
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Collections-Arrayed'
!

!AbstractNumberVector class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2011 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.
"
!

documentation
"
    abstract superclass for all direct storing number vector classes
    (float, double, integer arrays)

    Mostly to share double dispatch code.
"
! !

!AbstractNumberVector methodsFor:'queries'!

absMax
    "return the largest absolute value"

    |minMax|

    minMax := self minMax.
    ^ (minMax at:1) abs max:(minMax at:2) abs

    "
     |f1|

     f1 := (1 to:1000) asFloatArray.
     Time millisecondsToRun:[ 1000 timesRepeat:[ f1 absMax ] ]
    "

    "
     |f1|

     f1 := FloatArray withAll:#(1 2 3 4 5).
     f1 absMax
    "
    "
     |f1|

     f1 := FloatArray withAll:#(5 4 3 2 1).
     f1 absMax
    "
    "
     |f1|

     f1 := FloatArray withAll:#(5 -4 3 2 1).
     f1 absMax
    "
    "
     |f1|

     f1 := FloatArray withAll:#(5 -5 3 2 1).
     f1 absMax
    "
    "
     |f1|

     f1 := FloatArray withAll:#(5 -6 3 2 1).
     f1 absMax
    "
!

length
    "Return the length of the receiver interpreted as vector
     (that is the length of the vector from 0.0 @ 0.0 @ ... @ 0.0
      to the point in the n-dimensional space represented by the receiver)"

    ^ self squaredLength sqrt

    "
     #(10.0 10.0) asFloatArray length
     #(10.0 10.0 10.0) asFloatArray length
    "
!

squaredLength
    "Return the squared length of the receiver interpreted as vector"

    ^ self dot: self
! !

!AbstractNumberVector methodsFor:'vector arithmetic'!

dot:aFloatVector
    "Return the dot product of the receiver and the argument.
     Fail if the argument is not of the same size as the receiver."

    |mySize result|

    mySize := self size.
    mySize = aFloatVector size ifFalse:[^self error:'Must be of equal size'].
    result := 0.0.
    1 to: mySize do:[:i|
        result := result + ((self at: i) * (aFloatVector at: i)).
    ].
    ^result

    "
     |v|

     v := #(2.0 2.0 1.0) asDoubleArray.
     v dot:v.            
    "
! !

!AbstractNumberVector class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/AbstractNumberVector.st,v 1.3 2014-02-04 16:40:25 cg Exp $'
!

version_CVS
    ^ '$Header: /cvs/stx/stx/libbasic/AbstractNumberVector.st,v 1.3 2014-02-04 16:40:25 cg Exp $'
! !