Numerics

  • Thread starter Thread starter John Fourkiotis
  • Start date Start date
J

John Fourkiotis

Hello to everyone,
as a part of a big project, a Matrix (2D) class is needed that can handle
both double and complex values. For example, we should be able to write:
Matrix someMatrix = new ....
.....
someMatrix[0,3] = new Scalar(3); // [0,3] = 3, a double value
someMatrix[0,2] = new Complex(4, -2) // [0,2] = 4-2i, complex value

Although speed is not the top of my concerns, is there an efficient but
elegant way of accomplishing this ? I wouldnt like to use an object[] array,
ie

class Matrix
{
private Object[] data;
....
}

Can generics help with this kind of problem ?


Thanx
 
John Fourkiotis said:
Hello to everyone,
as a part of a big project, a Matrix (2D) class is needed that can handle
both double and complex values. For example, we should be able to write:
Matrix someMatrix = new ....
....
someMatrix[0,3] = new Scalar(3); // [0,3] = 3, a double value
someMatrix[0,2] = new Complex(4, -2) // [0,2] = 4-2i, complex value

Although speed is not the top of my concerns, is there an efficient but
elegant way of accomplishing this ? I wouldnt like to use an object[]
array,
ie

class Matrix
{
private Object[] data;
....
}

Since the real numbers are a subset of the complex numbers why not just use
Complex(3,0)
instead of
Scalar(3)
?

David
 
Interesting, Eran.

You used generics to get around the declaration issues of type, but then
hardcoded the array sizes for efficiency.
Not sure if this is what the original poster was asking about, but it is
definitely an interesting example.

Thanks for contributing. I found your post to be valuable.

--- Nick

Eran Kampf said:
You can use generics...
Doing arithmetics on generic type parameters is a problem but what you need
to do to avoid this is define an interface with the matrix arithmetics
(matrix addition, multiply etc), implement it for float, double, complex and
whatever other type you need and use these implementations from the generic
matrix class.

I've included the source code for a simple implementation I made to
benchmark performance...

You can check these related blog posts:
http://www.ekampf.com/blog/PermaLin...spx?guid=8626ee5a-3002-4e77-bfdd-e3a3f3d4a8e4

--
Eran Kampf
http://www.ekampf.com/blog
http://www.ekampf.com


John Fourkiotis said:
Hello to everyone,
as a part of a big project, a Matrix (2D) class is needed that can handle
both double and complex values. For example, we should be able to write:
Matrix someMatrix = new ....
....
someMatrix[0,3] = new Scalar(3); // [0,3] = 3, a double value
someMatrix[0,2] = new Complex(4, -2) // [0,2] = 4-2i, complex value

Although speed is not the top of my concerns, is there an efficient but
elegant way of accomplishing this ? I wouldnt like to use an object[]
array,
ie

class Matrix
{
private Object[] data;
....
}

Can generics help with this kind of problem ?


Thanx
 
Well, two answers come to mind. As David pointed out, Scalar numbers are a
subset of Complex numbers, so there's no reason why you cannot simply use
Complex number to represent all of the numbers of the matrix. It is easy
enough to interpret the results... any resulting value that has no imaginary
component is scalar.

The other answer is more general. You have a matrix and you want the data
type to vary, but you want the operations not to vary. You need to
encapsulate the variation. This part of the answer applies to all array
operations, and all types.

You create an abstract type that is able to perform all the basic operations
of either type in a graceful manner. Then you implement your abstract type
with two different classes: one for the first data type and the second class
to implement the second data type. Each class knows how to multiply itself
by an object of the abstract type, and add itself to an object of the
abstract type. These are the basic methods.

You declare an array container class to hold the array and to encode the
methods needed to perform larger operations by combining the basic methods
of the child types (in your case, matrix multiplication).

Create a simple factory object that creates the correct type.

This is called the strategy pattern. You can look it up in any design
patterns reference.

Hope this helps,
--- Nick
 
You do not have to hardcode the matrix size...
This is just a sample (I took the existing matrix class from my library and
modified it)
You can apply the same solution when using NxN matrices...

--
Eran Kampf
http://www.ekampf.com/blog
http://www.ekampf.com


Nick Malik said:
Interesting, Eran.

You used generics to get around the declaration issues of type, but then
hardcoded the array sizes for efficiency.
Not sure if this is what the original poster was asking about, but it is
definitely an interesting example.

Thanks for contributing. I found your post to be valuable.

--- Nick

Eran Kampf said:
You can use generics...
Doing arithmetics on generic type parameters is a problem but what you need
to do to avoid this is define an interface with the matrix arithmetics
(matrix addition, multiply etc), implement it for float, double, complex and
whatever other type you need and use these implementations from the generic
matrix class.

I've included the source code for a simple implementation I made to
benchmark performance...

You can check these related blog posts:
http://www.ekampf.com/blog/PermaLin...spx?guid=8626ee5a-3002-4e77-bfdd-e3a3f3d4a8e4

--
Eran Kampf
http://www.ekampf.com/blog
http://www.ekampf.com


John Fourkiotis said:
Hello to everyone,
as a part of a big project, a Matrix (2D) class is needed that can handle
both double and complex values. For example, we should be able to
write:
Matrix someMatrix = new ....
....
someMatrix[0,3] = new Scalar(3); // [0,3] = 3, a double value
someMatrix[0,2] = new Complex(4, -2) // [0,2] = 4-2i, complex value

Although speed is not the top of my concerns, is there an efficient but
elegant way of accomplishing this ? I wouldnt like to use an object[]
array,
ie

class Matrix
{
private Object[] data;
....
}

Can generics help with this kind of problem ?


Thanx
 
Back
Top