maths

  • Thread starter Thread starter David Sobey
  • Start date Start date
D

David Sobey

i'm looking to do some maths, such as parsing equations into polymorphic
trees, matrices, etc. I've done it in C, a mess, but now i want to port it
to something less low level (read: less messy). C++ is good, but will i get
much improvement, ie regarding the polymorphic trees (trees whoses node can
be
either a multiplication, subtraction, matrix etc)? Anyone have any
suggestions, or some unbiased ideas regarding a good, modern, well supported
language to try. C# seems to have awful support in this area, no TYPEDEF
AGHHHH! lol. Haskell is good, no newsgroup support though. Maybe Java?

Cheers
dave

--
"Aristotle said that some people were
only fit to be slaves. I do not
contradict him. But I reject slavery
becase I see no people fit to be masters"
C.S Lewis
 
David Sobey said:
i'm looking to do some maths, such as parsing equations into polymorphic
trees, matrices, etc. I've done it in C, a mess, but now i want to port it
to something less low level (read: less messy). C++ is good, but will i get
much improvement, ie regarding the polymorphic trees (trees whoses node can
be
either a multiplication, subtraction, matrix etc)? Anyone have any
suggestions, or some unbiased ideas regarding a good, modern, well supported
language to try. C# seems to have awful support in this area, no TYPEDEF
AGHHHH! lol. Haskell is good, no newsgroup support though. Maybe Java?

Well, Java doesn't have typedef either. Do you really *need* typedef?
If that's one of your non-obvious requirements, perhaps you could list
your others?
 
well, i used typedefs to make my basic data structures, ie matrices. i don't
want to be writing int[][] over and over again. and i don't really want to
have every node (ie a multiplication or addition node) as a class, i'd think
it's best to just have them as a compound data type. so i don't think this
can be done in c#. do you see where i'm heading with this?

cheers
dave
 
Hi David,

Maybe defining your own structures (value types with value semantics) could
be an option...

Hope this helps,
Regards,
Leon
 
sorry to have to play dumb but what does "defining your own structures
(value types with value semantics)" mean? an example, ie for a tree node or
a simple matrix array def would be nice.

thanks
dave
 
ahh ok you mean use structs. fair enough, but how would i get around things
like defining matrix arrays, i can't keep on going int[][], and i can't put
it in a struct, because then i would end up passing single member structs
everywhere, which is stupid.
 
David Sobey said:
ahh ok you mean use structs. fair enough, but how would i get around things
like defining matrix arrays, i can't keep on going int[][], and i can't put
it in a struct, because then i would end up passing single member structs
everywhere, which is stupid.

No it's not. It's encapsulation. Where would you put the methods to act
on those matrices? It makes perfect sense to put them in the
class/struct which encapsulates the matrix.
 
hmmm you could be right! *brain starts ticking....*

thanks for your help!
cheers
dave
 
So, what was wrong with using this?

<code>
/// <summary>
/// Stores a 3 row by 3 column matrix representing a geometric transform.
/// Not inheritable.
/// </summary>
public sealed
class AffineMatrix
{

/// <summary>
/// Creates an <code>AffineMatrix</code> with the elements of the identity
matrix.
/// </summary>
public AffineMatrix ( )
: this ( new float [0x6] { 1F, 0F, 0F, 1F, 0F, 0F } )
{
}

/// <summary>
/// Creates an <code>AffineMatrix</code> with the specified elements.
/// </summary>
/// <param name="m11">The value in the first row and first column.</param>
/// <param name="m12">The value in the first row and second
column.</param>
/// <param name="m21">The value in the second row and first
column.</param>
/// <param name="m22">The value in the second row and second
column.</param>
/// <param name="m31">The value in the third row and first column.</param>
/// <param name="m32">The value in the third row and second
column.</param>
public AffineMatrix
( float m11, float m12, float m21, float m22, float m31, float m32 )
: this ( new float [0x6] { m11, m12, m21, m22, m31, m32 } )
{
}

private AffineMatrix ( float [] matrix )
{
this.matrix = matrix;
}

~AffineMatrix ( )
{
matrix = null;
}


#region Internal Members

/// <summary>
/// The six-element array that represent this <code>AffineMatrix</code>.
/// </summary>
internal float [] matrix = null;

#endregion


/// <summary>
/// Gets a six-element array that represent this
<code>AffineMatrix</code>.
/// </summary>
public float [] Elements
{
get
{
return matrix;
}
}

/// <summary>
/// Gets the current translation on the horizontal axis.
/// </summary>
public float OffsetX
{
get
{
return matrix [0x4];
}
}

/// <summary>
/// Gets the current translation on the vertical axis.
/// </summary>
public float OffsetY
{
get
{
return matrix [0x5];
}
}

/// <summary>
/// Gets a value indicating whether this <code>AffineMatrix</code>
/// equals the identity matrix.
/// </summary>
public bool IsIdentity
{
get
{
return 1F == matrix [0x0] && 0F == matrix [0x1]
&& 0F == matrix [0x2] && 1F == matrix [0x3]
&& 0F == matrix [0x4] && 0F == matrix [0x5];
}
}


/// <summary>
/// Resets this <code>AffineMatrix</code> to have the elements of the
identity matrix.
/// </summary>
public void Reset ( )
{
matrix [0x5]
= matrix [0x4]
= matrix [0x2]
= matrix [0x1]
= 0F;
matrix [0x3]
= matrix [0x0]
= 1F;
}

/// <summary>
/// Offsets the coordinates of this <code>AffineMatrix</code>.
/// </summary>
/// <param name="dx">Specifies the horizontal translation.</param>
/// <param name="dy">Specifies the vertical translation.</param>
public void Translate ( float dx, float dy )
{
matrix [0x4] = dx;
matrix [0x5] = dy;
}

/// <summary>
/// Applies a scale vector to this <code>AffineMatrix</code>.
/// </summary>
/// <param name="sx">Specifies the horizontal scale factor.</param>
/// <param name="sy">Specifies the vertical scale factor.</param>
public void Scale ( float sx, float sy )
{
matrix [0x0] = sx;
matrix [0x3] = sy;
}

/// <summary>
/// Applies a rotation to this <code>AffineMatrix</code>.
/// </summary>
/// <param name="sx">Specifies the horizontal scale factor.</param>
/// <param name="sy">Specifies the vertical scale factor.</param>
public void Rotate ( float sx, float sy )
{
/*
Rotations are produced by [cos 0 sin 0 -sin 0 cos 0 0 0], which has the
effect
of rotating the coordinate system axes by an angle 0 counterclockwise.
*/
// matrix [0x0] = sx;
// matrix [0x3] = sy;
throw new System.NotImplementedException();
}

/// <summary>
/// Creates an exact copy of this <code>AffineMatrix</code>.
/// </summary>
/// <returns>An instance copy.</returns>
public AffineMatrix Clone ( )
{
return new AffineMatrix( new float [0x6]
{ matrix [0x0], matrix [0x1]
, matrix [0x2], matrix [0x3]
, matrix [0x4], matrix [0x5] } );
}


}

</code>
 
Back
Top