You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Tzvika Barenholz <tz...@gmail.com> on 2005/01/09 16:55:00 UTC

[commons - math] Vector and Scalar multiplications

Hi all
Is there somewhere in the Math package simple methods for multiplying
vectors with other vectors of scalars? this is simple functionality
but it should be in once place IMHO, not as a private little method in
every Class.

Did i miss something? as far as I could tell there is no RealVector
class or the like at all.

thanks
Tzvika

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [commons - math] Vector and Scalar multiplications

Posted by Kim van der Linde <ki...@kimvdlinde.com>.
I think it might be wise to use the JAMA implementation for this, they 
are completly free of copyright and related stuff and they are easy 
implemented (as I have done) in commons.math

Kim

Tzvika Barenholz wrote:

>>The formulas that you cite look correct mathematically, but
>>unfortunately not the best numerically. The reference in the post above
>>describes a better computational approach.  Patches to support QR
>>decomposition of real matrices so we can implement that algorithm or
>>other numerically sound multiple regression approaches would be appreciated.
>>
>>Phil
> 
> 
> 
> not to sound too dumb, but does anyone have a good reference on what
> this QR decomposition actually is? I'd love to look at it.
> 
> By the way, i've written some tests for my own RealMatrix-based
> implementation of multiple linear regression, and the numerics aren't
> too bad at all as far as i could tell (first differences appearing
> around the 12th digit after the decimal point or so). that is, without
> a more comprehensive test.  Do you guys maybe have a requirement for
> how accurate an algorithm must be to be acceptable?
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
> 

-- 
http://www.kimvdlinde.com

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [commons - math] Vector and Scalar multiplications

Posted by Tzvika Barenholz <tz...@gmail.com>.
> The formulas that you cite look correct mathematically, but
> unfortunately not the best numerically. The reference in the post above
> describes a better computational approach.  Patches to support QR
> decomposition of real matrices so we can implement that algorithm or
> other numerically sound multiple regression approaches would be appreciated.
> 
> Phil


not to sound too dumb, but does anyone have a good reference on what
this QR decomposition actually is? I'd love to look at it.

By the way, i've written some tests for my own RealMatrix-based
implementation of multiple linear regression, and the numerics aren't
too bad at all as far as i could tell (first differences appearing
around the 12th digit after the decimal point or so). that is, without
a more comprehensive test.  Do you guys maybe have a requirement for
how accurate an algorithm must be to be acceptable?

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [commons - math] Vector and Scalar multiplications

Posted by Phil Steitz <ph...@steitz.com>.
Tzvika Barenholz wrote:
> On Sun, 09 Jan 2005 14:11:31 -0500, Phil Steitz <ph...@steitz.com> wrote:
> 
>>... I would not be
>>averse to adding either a set of static methods on double[] arrays or a
>>RealVector class for various vector products, linear combinations, etc.
>>if others have practical uses for this.
> 
> 
> 
> Regarding practical uses: I started working on a
> MultipleLinearRegression class (with m explaining variables and n
> observations) and the process derived from the formula of calculating
> the coefficients vector has some repetative code for mutliplying
> vectors by scalars as well as adding/subtracting/averaging vectors.
> The formulas (which also require inverting a matrix and matrix*vector
> opreation by the way) can be found, if in simplified forms, on the Dr.
> Math site: http://mathforum.org/library/drmath/view/51892.html

I would be happy to review and work with you on patches supporting 
vector operations directly.  Regarding the application above, however, 
see the discussion here:
http://nagoya.apache.org/eyebrowse/ReadMsg?listName=commons-dev@jakarta.apache.org&msgId=1330648

The formulas that you cite look correct mathematically, but 
unfortunately not the best numerically. The reference in the post above 
describes a better computational approach.  Patches to support QR 
decomposition of real matrices so we can implement that algorithm or 
other numerically sound multiple regression approaches would be appreciated.

Phil


> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [commons - math] Vector and Scalar multiplications

Posted by Tzvika Barenholz <tz...@gmail.com>.
On Sun, 09 Jan 2005 14:11:31 -0500, Phil Steitz <ph...@steitz.com> wrote:
> ... I would not be
> averse to adding either a set of static methods on double[] arrays or a
> RealVector class for various vector products, linear combinations, etc.
> if others have practical uses for this.


Regarding practical uses: I started working on a
MultipleLinearRegression class (with m explaining variables and n
observations) and the process derived from the formula of calculating
the coefficients vector has some repetative code for mutliplying
vectors by scalars as well as adding/subtracting/averaging vectors.
The formulas (which also require inverting a matrix and matrix*vector
opreation by the way) can be found, if in simplified forms, on the Dr.
Math site: http://mathforum.org/library/drmath/view/51892.html

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [commons - math] Vector and Scalar multiplications

Posted by "J.Pietschmann" <j3...@yahoo.de>.
Phil Steitz wrote:
> I think what you really mean here is the function call / object 
> instantiation overhead, right?

Well, no. I'm interested in background information on hot run
time optimizers work now, i.e. whether they will optimize
  for(i=0;i<n;i++) {
   a.set(i,a.get(i).add(b));
  }
into
  for(i=0;i<n;i++) {
   for(j=0;j<m;j++) {
     a[i][j]+=b[j];
   }
  }
including optimizing array boundary checks away, doing loop
unrolling etc., and what the conditions are for these optimizations
to kick in. Sun Java 1.3 apparently had limited capabilities
in this area.

J.Pietschmann

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [commons - math] Vector and Scalar multiplications

Posted by Phil Steitz <ph...@steitz.com>.
J.Pietschmann wrote:
> Tzvika Barenholz wrote:
> 
>>> There are multiple (conflicting) definitions
>>> for vector*vector in various contexts, the most common is
>>> (a1, a2, ...)*(b1, b2, ...)=(a1*b1, a2*b2, ...)
> 
> ...
> 
>> I mean exactly the above indicated 'common one'. of course i could
>> relate to it as a multiplication of two matrices (the left a row the
>> right a column) but seems a bit ugly.
> 
> 
> You'll get a 1x1 matrix in this case, i.e. you geft the scalar
> product of two vectors: a1*b1+a2*b2+...
> 
>> Do you think there is a point in adding a sort of static methods class
>> , VectorUtils or the like?
> 
> 
> I never felt the need to do this. In Java 1.3 even with HotSpot,
> there is still a noticable speed advantage if you unroll and
> inline simple vector operations in inner loops instead of calling
> a function.

I have not seen a compelling need to do this anywhere within [math] now, 
and I would also tend to shy away from the overhead of creating objects 
/ calling functions for simple things like dot products; but I am not 
sure that is actually a valid concern (see below) and I would not be 
averse to adding either a set of static methods on double[] arrays or a 
RealVector class for various vector products, linear combinations, etc. 
if others have practical uses for this.
> 
> Does anybody have more recent data about Java performance
> for numerical comuptations?

I think what you really mean here is the function call / object 
instantiation overhead, right?  The significance of this will obviously 
vary with the length of the vectors (longer = more actual comp to spread 
the overhead) and what you are doing. I remember being surprised by the 
results of some benchmarks that Mark and I ran last year when we were 
debating "monolithic" static methods vs. more modular code in the stat 
package. Might be interesting to run some benchmarks on dot products 
computed inline vs static double[] methods vs using objects.

Phil
> 
> J.Pietschmann
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [commons - math] Vector and Scalar multiplications

Posted by "J.Pietschmann" <j3...@yahoo.de>.
Tzvika Barenholz wrote:
>>There are multiple (conflicting) definitions
>>for vector*vector in various contexts, the most common is
>> (a1, a2, ...)*(b1, b2, ...)=(a1*b1, a2*b2, ...)
...
> I mean exactly the above indicated 'common one'. of course i could
> relate to it as a multiplication of two matrices (the left a row the
> right a column) but seems a bit ugly.

You'll get a 1x1 matrix in this case, i.e. you geft the scalar
product of two vectors: a1*b1+a2*b2+...

> Do you think there is a point in adding a sort of static methods class
> , VectorUtils or the like?

I never felt the need to do this. In Java 1.3 even with HotSpot,
there is still a noticable speed advantage if you unroll and
inline simple vector operations in inner loops instead of calling
a function.

Does anybody have more recent data about Java performance
for numerical comuptations?

J.Pietschmann

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [commons - math] Vector and Scalar multiplications

Posted by Tzvika Barenholz <tz...@gmail.com>.
> What exactly do you mean by "methods for multiplying vectors with
> other vectors of scalars"? General vector algebra usually only
> defines scalar*vector. There are multiple (conflicting) definitions
> for vector*vector in various contexts, the most common is
>  (a1, a2, ...)*(b1, b2, ...)=(a1*b1, a2*b2, ...)
> 
> Could you be more specific?
> 
> J.Pietschmann


I mean exactly the above indicated 'common one'. of course i could
relate to it as a multiplication of two matrices (the left a row the
right a column) but seems a bit ugly.

Do you think there is a point in adding a sort of static methods class
, VectorUtils or the like?

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [commons - math] Vector and Scalar multiplications

Posted by "J.Pietschmann" <j3...@yahoo.de>.
Tzvika Barenholz wrote:
> Is there somewhere in the Math package simple methods for multiplying
> vectors with other vectors of scalars?

No.

> Did i miss something? as far as I could tell there is no RealVector
> class or the like at all.

That's probably because there are already two common representations
of a vector:
1. a Java array
2. a special case of a matrix with either one row or one column.

What exactly do you mean by "methods for multiplying vectors with
other vectors of scalars"? General vector algebra usually only
defines scalar*vector. There are multiple (conflicting) definitions
for vector*vector in various contexts, the most common is
  (a1, a2, ...)*(b1, b2, ...)=(a1*b1, a2*b2, ...)

Could you be more specific?

J.Pietschmann

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org