We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › class Matrix, MatrixMath for neural network
Pages: 1 2 
class Matrix, MatrixMath for neural network (Read 2753 times)
class Matrix, MatrixMath for neural network
Nov 8th, 2009, 3:56am
 
 I have posted this in the syntax section but I m not sure if it the good section so I also post here.
I'm trying at present to construct classes presented by jeff heaton in his book introduction to neural network for java : http://www.heatonresearch.com/online/introduction-neural-networks-java-edition-2...
Those classes are useful for every example of neural network jeff heaton give in his book so I need them anyway.
Here is what I tried to do(probably an aberration):
Code:


Code:
class  Matrix 
{    
 private int r;
 private int c;
 private double[][] M;

public Matrix(final double[][]input)
 {
   M = input;
 }

 public void add(int row, int col, double value)
 {
    r = row;
    c = col;
   for(int i = 0; i<r; i++){
     for(int j = 0; j<c; j++){
       M[i][j] = value;
     }
   }
 }
}

public static class MatrixMath{
public  static Matrix add(final Matrix m1,final Matrix m2)
 {
   final double result[][] = new double[m1.getRows()][m1.getCols()];
   for (int resultRow = 0; resultRow < m1.getRows(); resultRow++) {
for (int resultCol = 0; resultCol < m1.getCols(); resultCol++) {
      result[resultRow][resultCol] = m1.get(resultRow, resultCol)+ m2.get(resultRow, resultCol);
}
}
return new Matrix(result);
 }
 
}





Does someone have an idea how I could define my classes?
Especially MatrixMath class, I don't know how to deal with.
Jeff Heaton don't talk about constructor and use only static methods in this class. What I made is probably stupid but I don't know where.
thanks,
théo
Re: class Matrix, MatrixMath for neural network
Reply #1 - Nov 8th, 2009, 4:33am
 
i wouldnt post in two categories, especially not in syntax and programm as people mix that anyway all the time. so if  you still need to, please just post a link to your other threat. so people dont answer two times.
Re: class Matrix, MatrixMath for neural network
Reply #2 - Nov 8th, 2009, 4:40am
 
Ok, sorry, I removed the one in the syntax section
théo
Re: class Matrix, MatrixMath for neural network
Reply #3 - Nov 9th, 2009, 5:32am
 
Hello, I am actually one of the Encog developers, which uses that class.  

Jeff's MatrixMath class works much like the Java Math class.  You would never do something like:

Math m = new Math();

Its a static class, with a private constructor, so you just don't do it that way.

MatrixMath works more like:

Matrix result = MatrixMath.add(matrixa,matrixb);

Don't try to instantiate it, just call the static methods like I did above.

There is also a forum for discussing these books specifically, and other AI topics here:
Re: class Matrix, MatrixMath for neural network
Reply #4 - Nov 11th, 2009, 3:29am
 
Hi SimaSingh86,
Encog seems to be a georgous library, but quite far for my understanding by now, I guess.
Thank you for your explanation of the use of the MatrixMath class.
The link for the forum didn't appeared, could you post it again ?

Re: class Matrix, MatrixMath for neural network
Reply #5 - Nov 11th, 2009, 4:35am
 
Another stupid question, what I m doing wrong with that:
Code:
public static class MatrixMath{

public static Matrix add(final Matrix m1,final Matrix m2) {
 
   final double result[][] = new double[m1.getRows()][m1.getCols()];
     
   for (int resultRow = 0; resultRow < m1.getRows(); resultRow++) {
for (int resultCol = 0; resultCol < m1.getCols(); resultCol++) {
      result[resultRow][resultCol] = m1.get(resultRow, resultCol)+ m2.get(resultRow, resultCol);
}
}
m = new Matrix(result);
return m;
 }
}

wich give me"Cannot make a static reference to the non-static field m"
Re: class Matrix, MatrixMath for neural network
Reply #6 - Nov 11th, 2009, 5:46am
 
You should put such class (which doesn't use the PApplet context anyway) in a Java tab (ie. a .java file): all classes in .pde files are internal to the main sketch's class, and they can't use static fields. In a .java file, such classes are OK.
Re: class Matrix, MatrixMath for neural network
Reply #7 - Nov 11th, 2009, 7:00am
 
Ok, thanks PhiLo,
but do you think there is an other way I could define this class in the PApplet context, without static fields(I am afraid of pure Java)?

Re: class Matrix, MatrixMath for neural network
Reply #8 - Nov 11th, 2009, 7:21am
 
Don't be afraid, Processing is pure Java (almost!), with a bit of sugar on top to make it more edible.
The code you show is actually pure Java...
If you put it in a Java tab, it will be integrated to the sketch, so you still don't have to define the main class, the main() method, etc.

Note: Processing doesn't like much doubles, unless you need high precision, perhaps stick to float type.
Re: class Matrix, MatrixMath for neural network
Reply #9 - Nov 11th, 2009, 8:04am
 
Thank you PhiLo, I will try the way you said : without sugar.
And put floats instead of doubles, I don't think I need them.
Re: class Matrix, MatrixMath for neural network
Reply #10 - Nov 13th, 2009, 8:33am
 
So I tryed without sugar and it almost work. Still one thing I don't understand, it's how I can make severals constructors for a Matrix class, squared matrix, one column or one row matrix and define the methods like getRows on it with sometimes a 2D array and sometimes a 1D array?
Re: class Matrix, MatrixMath for neural network
Reply #11 - Nov 13th, 2009, 9:08am
 
Quote:
how I can make severals constructors for a Matrix class

Just declare them. As long as they have different parameters, the compiler will know which one you use.
Re: class Matrix, MatrixMath for neural network
Reply #12 - Nov 13th, 2009, 9:41am
 
Yes, but does it mean I have to make several methods for one operation ?
like :
Code:
public static Matrix multiply(final Matrix m1, float m)
 {
   final float result[][] = new float[m1.getRows()][m1.getCols()];
   for (int row = 0; row < m1.getRows(); row++) {
     for (int col = 0; col < m1.getCols(); col++) {
       result[row][col] = m1.get(row, col) * m;
     }
   }
   Matrix m2 = new Matrix(result);
   return m2;
 }
public static Matrix multiply(final Matrix m1, float m)
 {
   final float result[] = new float[m1.length];
   for (int i = 0; i< m1.length; i++) {
       result[i] = m1.get(i) * m;
     }
   Matrix m2 = new Matrix(result);
   return m2;
 }

and the same for every operations?
Re: class Matrix, MatrixMath for neural network
Reply #13 - Nov 13th, 2009, 1:28pm
 
Ah, so that's the way around...
You can't, precisely, do what you show, because the parameters are identical.
Not sure what is the best approach (without precise specs...), but maybe you can have a base Matrix class (perhaps abstract) and concrete classes, for one row matrix, one column matrix, regular matrix.
Or, on the other hand, consider seeing the first two types as a sub-type of the latter, ie. implement them with a generic Matrix class. This solution might lead to less code duplication.
Or make three (or more) classes but defer the implementation of special matrices to calls of the generic Matrix, with proper parameters.
Re: class Matrix, MatrixMath for neural network
Reply #14 - Nov 14th, 2009, 4:29am
 
thank you so much PhiLho, you help me a lot.
The second solution attracts me but I am not sure what you mean by generic Matrix class. And are you talking about parent class and subclasses? Sorry for my slow understanding...
Pages: 1 2