I need to make a method that receives the matrix values as a string as
[code]"[ 1/4 1/3 1/2; 2/3 1/5 2/5; 1/6 1/5 7/8 ] "[/code]
and initializes the matrix ( ex. “[ 1/4 1/3 1/2 ; 2/3 1/5 2/5 ; 1/6 1/5 7/8 ] “)
I don't how to implement the functions parameters to accept this string...
Any help would be appreciated.
here's the code:
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HomeWork1
{
class FractionMatrix
{
private Fraction [,] arr = new Fraction[3, 3];
Fraction a11 = new Fraction();
Fraction a12 = new Fraction();
Fraction a13 = new Fraction();
Fraction a21 = new Fraction();
Fraction a22 = new Fraction();
Fraction a23 = new Fraction();
Fraction a31 = new Fraction();
Fraction a32 = new Fraction();
Fraction a33 = new Fraction();
public FractionMatrix()
{
Fraction frac = new Fraction();
frac.SetFraction(0, 1);
}
// A method that receives the matrix values as a string
public void SetValues(string value)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
arr[i, j] = Int32.Parse(value);
}
}
}
// A 2Dimensional indexer to set and return the value
// of any matrix element as a string (ex.A[1,1]=“1/3”)
public void SetValue(string value)
{
}
}
class Program
{
static void Main(string[] args)
{
FractionMatrix A = new FractionMatrix ();
FractionMatrix B = new FractionMatrix ();
A.SetValues("[ 1/6 1/3 1/2; 2/3 1/5 2/5; 1/6 1/5 7/8 ] ");
B.SetValues("[ 1/4 2/3 1/2; 4/3 10/5 7/5; 4/5 4/3 9/10 ] ");
//A[1,1].SetValue("4/13");
//Console.WriteLine("{0} + {1} = {2}\n\n\n", A, B, A + B);
//Console.WriteLine("{0} * {1} = {2}\n\n\n", A, B, A * B);
//Console.WriteLine("det(A) = {0} + det(B) = {1}", A.Det(), B.Det());
Console.Read();
}
}
}
[/code]
I'm having problems with this function:
[code]
// A method that receives the matrix values as a string
public void SetValues(string value)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
arr[i, j] = Int32.Parse(value);
}
}
}
[/code]
Loading
AlanPosted Apr 10, 2008, 10:57 AM
I've inserted some code below for the ComplexNumber constructor which takes a string as an argument. In order to do this, it was necessary to add a helper method to parse the real and imaginary parts in isolation.
Indexers are only relevant in the case of collection classes to enable you to index the collection as if it were an array. As a complex number isn't really a collection, you don't need one here.
You really need to write a 'recursive descent parser'to parse an arbitrary complex number expression which is not a trivial undertaking. However, working on the convention that the complex numbers will not have any spaces in them but the operators will have spaces either side (as in the last three expressions in your Main() method) it is possible to write a simple parser which I've done below in the Calculate() method. I've assumed that you'd want multiplication to have precedence over addition/subtraction as in normal arithmetic expressions.
As it's easier to work backwards when evaluating binary expressions, it was necessary to change all subtractions into additions which required that an overload of the unary minus operator be added to the ComplexNumber class.
Incidentally, if they're expecting you to figure all this out in exam conditions without having done anything like it before, then I'm not surprised you've failed it!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test_1_1
{
abstract class RealNumber
{
protected float real;
public abstract float Real { get; }
}
class ComplexNumber : RealNumber
{
private float imaginary;
// helper method to parse a pure real or pure imaginary number
private void ParseElement(string value)
{
if (value.IndexOf('i') > -1)
{
if (value == "i" || value == "+i")
{
imaginary = 1.0f;
}
else if (value == "-i")
{
imaginary = -1.0f;
}
else
{
imaginary = float.Parse(value.Replace("i", ""));
}
}
else
{
real = float.Parse(value);
}
}
public ComplexNumber()
{
real = 0.0f;
imaginary = 0.0f;
}
public ComplexNumber(float realValue, float imaginaryValue)
{
real = realValue;
imaginary = imaginaryValue;
}
public ComplexNumber(string value)
{
if (value == null || value.Trim() == "")
{
real = 0.0f;
imaginary = 0.0f;
}
else
{
value = value.Replace(" ", ""); // remove spaces
char[] ca = new char[]{'+','-'};
int index = value.IndexOfAny(ca, 1); // ignore first character
if (index == -1)
{
ParseElement(value);
}
else
{
string temp1 = value.Substring(0, index);
string temp2 = value.Substring(index);
ParseElement(temp1);
ParseElement(temp2);
}
}
}
public override float Real
{
get
{
return real;
}
}
public float Imaginary
{
get
{
return imaginary;
}
}
public static ComplexNumber operator +(ComplexNumber complex1, ComplexNumber complex2)
{
ComplexNumber result = new ComplexNumber();
result.real = complex1.real + complex2.real;
result.imaginary = complex1.imaginary + complex2.imaginary;
return result;
}
public static ComplexNumber operator -(ComplexNumber complex1, ComplexNumber complex2)
{
ComplexNumber result = new ComplexNumber();
result.real = complex1.real - complex2.real;
result.imaginary = complex1.imaginary - complex2.imaginary;
return result;
}
// unary minus
public static ComplexNumber operator -(ComplexNumber complex)
{
complex.real = -complex.real;
complex.imaginary = -complex.imaginary;
return complex;
}
public static ComplexNumber operator *(ComplexNumber complex1, ComplexNumber complex2)
{
ComplexNumber result = new ComplexNumber();
result.real = complex1.real * complex2.real - complex1.imaginary * complex2.imaginary;
result.imaginary = complex1.real * complex2.imaginary + complex1.imaginary * complex2.real;
return result;
}
public override string ToString()
{
return "( " + real + ( imaginary < 0 ? " - " + ( imaginary * -1 ) : " + " + imaginary ) + "i )";
}
}
class ComplexNumberCalculator
{
public ComplexNumberCalculator()
{
}
public ComplexNumber Calculate(string value)
{
// split 'value' into operands and operators
List
// do multiplications first in reverse
for (int i = list.Count - 2; i >= 1; i -= 2)
{
if (list[i].ToString() == "*")
{
c = (ComplexNumber)list[i-1] * (ComplexNumber)list[i+1];
list.RemoveAt(i + 1);
list.RemoveAt(i);
list[i-1] = c;
}
}
if (list.Count == 1) return (ComplexNumber)list[0];
// now change all subtractions to additions
for (int i = 1; i < list.Count - 1; i += 2)
{
if (list[i].ToString() == "-")
{
list[i] = "+";
list[i+1] = -(ComplexNumber)list[i+1];
}
}
// now do additions in reverse
for (int i = list.Count - 2; i >= 1; i -= 2)
{
c = (ComplexNumber)list[i-1] + (ComplexNumber)list[i+1];
list.RemoveAt(i + 1);
list.RemoveAt(i);
list[i-1] = c;
}
return (ComplexNumber)list[0];
}
}
class Program
{
static void Main(string[] args)
{
ComplexNumber a = new ComplexNumber("2 + 4i");
ComplexNumber b = new ComplexNumber("5 + 3i");
Console.WriteLine(a * b);
ComplexNumberCalculator cc = new ComplexNumberCalculator();
Console.WriteLine(cc.Calculate("1+3i + 2+4i * 5+3i"));
Console.WriteLine(cc.Calculate("2-2i * 2.2+7.8i * 7.1+2i + 4.5+1.1i"));
Console.WriteLine(cc.Calculate("1-1i - 4.7+3i + 11+15.1i - 4.5+1.1i + 5-7i"));
Console.Read();
}
}
}
Janett XPosted Apr 9, 2008, 7:41 PM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test_1_1
{
abstract class RealNumber
{
protected float real;
public abstract float Real { get; }
}
class ComplexNumber : RealNumber
{
private float imaginary;
public ComplexNumber()
{
real = 0.0f;
imaginary = 0.0f;
}
public ComplexNumber(float realValue, float imaginaryValue)
{
real = realValue;
imaginary = imaginaryValue;
}
public ComplexNumber(string value)
{
}
public override float Real
{
get
{
return real;
}
}
public float Imginary
{
get
{
return imaginary;
}
}
public static ComplexNumber operator +(ComplexNumber complex1, ComplexNumber complex2)
{
ComplexNumber result = new ComplexNumber();
result.real = complex1.real + complex2.real;
result.imaginary = complex1.imaginary + complex2.imaginary;
return result;
}
public static ComplexNumber operator -(ComplexNumber complex1, ComplexNumber complex2)
{
ComplexNumber result = new ComplexNumber();
result.real = complex1.real - complex2.real;
result.imaginary = complex1.imaginary - complex2.imaginary;
return result;
}
public static ComplexNumber operator *(ComplexNumber complex1, ComplexNumber complex2)
{
ComplexNumber result = new ComplexNumber();
result.real = complex1.real * complex2.real;
result.imaginary = complex1.imaginary * complex2.imaginary;
return result;
}
public override string ToString()
{
return "( " + real + ( imaginary < 0 ? " - " + ( imaginary * -1 ) : " + " + imaginary ) + "i )";
}
}
class ComplexNumberCalculator
{
public ComplexNumberCalculator()
{
}
public ComplexNumber Calculate(string value)
{
}
}
class Program
{
static void Main(string[] args)
{
ComplexNumber a = new ComplexNumber("2 + 4i");
ComplexNumber b = new ComplexNumber("5 + 3i");
Console.WriteLine(a * b);
ComplexNumberCalculator cc = new ComplexNumberCalculator();
Console.WriteLine(cc.Calculate("1+3i + 2+4i * 5+3i"));
Console.WriteLine(cc.Calculate("2-2i * 2.2+7.8i * 7.1+2i + 4.5+1.1i"));
Console.WriteLine(cc.Calculate("1-1i - 4.7+3i + 11+15.1i - 4.5+1.1i + 5-7i"));
Console.Read();
}
}
}
AlanPosted Apr 8, 2008, 11:34 AM
Well, all your formulas look OK to me.
However, I suspect that the problem may be that the 'int' type, which can only accomodate numbers with an absolute magnitude of around 2 billion is too small to deal with the huge numbers that can arise when you're dealing with 'unreduced' fractions in a chain of calculations such as the one you're doing for a determinant of a 3x3 matrix.
Assuming you have the numerator and denominator fields of the Fraction class defined as 'int' , try changing them to 'long' instead.
Janett XPosted Apr 8, 2008, 9:47 AM
///
/// Operators for the Fraction object
/// includes binary opertors such as +,-,*,/
///
public static Fraction operator +(Fraction frac1, Fraction frac2)
{
Fraction temp = new Fraction();
temp.numerator = (frac1.numerator * frac2.denominator) + (frac2.numerator * frac1.denominator);
temp.denominator = frac1.denominator * frac2.denominator;
return temp;
}
public static Fraction operator -(Fraction frac1, Fraction frac2)
{
Fraction temp = new Fraction();
temp.numerator = (frac1.numerator * frac2.denominator) - (frac2.numerator * frac1.denominator);
temp.denominator = frac1.denominator * frac2.denominator;
return temp;
}
public static Fraction operator *(Fraction frac1, Fraction frac2)
{
Fraction temp = new Fraction();
temp.numerator = frac1.numerator * frac2.numerator;
temp.denominator = frac1.denominator * frac2.denominator;
return temp;
}
AlanPosted Apr 8, 2008, 8:39 AM
The only way that's going to work is if you've overloaded the '*', '+' and '-' operators for your Fraction class as all the elements of the matrix are Fraction objects.
Is that something which you have covered yet in your studies?
Janett XPosted Apr 7, 2008, 11:05 PM
///
/// The function returns the determinent of a Matrix object as Fraction
///
public Fraction Det()
{
Fraction det = new Fraction();
det = ((element[0, 0] * (element[1, 1] * element[2, 2] - element[2, 1] * element[1, 2])) -
(element[0, 1] * (element[1, 0] * element[2, 2] - element[2, 0] * element[1, 2])) +
(element[0, 2] * (element[1, 0] * element[2, 1] - element[2, 0] * element[1, 1])));
return det;
}
AlanPosted Apr 7, 2008, 4:57 PM
OK try this. I've had to add an outline Fraction class so I could test whether this was working properly or not:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HomeWork1
{
// add outline Fraction class
class Fraction
{
private int numerator;
private int denominator;
public void SetFraction(string fracString)
{
string[] temp = new string[2];
temp = fracString.Split('/');
numerator = Int32.Parse(temp[0]);
denominator = Int32.Parse(temp[1]);
}
public override string ToString()
{
return numerator.ToString() + "/" + denominator.ToString();
}
}
class FractionMatrix
{
private Fraction [,] arr = new Fraction[3, 3];
/* Don't need this lot
Fraction a11 = new Fraction();
Fraction a12 = new Fraction();
Fraction a13 = new Fraction();
Fraction a21 = new Fraction();
Fraction a22 = new Fraction();
Fraction a23 = new Fraction();
Fraction a31 = new Fraction();
Fraction a32 = new Fraction();
Fraction a33 = new Fraction();
public FractionMatrix()
{
Fraction frac = new Fraction();
frac.SetFraction(0, 1);
}
*/
// A method that receives the matrix values as a string
public void SetValues(string value)
{
// get rid of [] and any leading or trailing space
value = value.Replace("[","").Replace("]","").Trim();
// split string into three at the semicolon separator
string[] temp1 = new string[3];
temp1 = value.Split(';');
// declare temporary array needed in for loops
string[] temp2 = new string[3];
for (int i = 0; i < 3; i++)
{
// split each string in the array at the space separator
// after getting rid of any leading spaces
temp2 = temp1[i].TrimStart().Split(' ');
for (int j = 0; j < 3; j++)
{
Fraction frac = new Fraction();
frac.SetFraction(temp2[j]);
arr[i, j] = frac;
}
}
}
// A 2Dimensional indexer to set and return the value
// of any matrix element as a string (ex.A[1,1]=“1/3”)
public Fraction this[int x, int y]
{
get
{
if ( x >=0 && x < 3 && y >= 0 && y < 3)
{
return arr[x,y];
}
else
{
Console.WriteLine("Element is out of bounds");
return null;
}
}
set
{
if ( x >=0 && x < 3 && y >= 0 && y < 3)
{
arr[x,y] = value;
}
else
{
Console.WriteLine("Element is out of bounds");
}
}
}
}
class Program
{
static void Main(string[] args)
{
FractionMatrix A = new FractionMatrix ();
FractionMatrix B = new FractionMatrix ();
A.SetValues("[ 1/6 1/3 1/2; 2/3 1/5 2/5; 1/6 1/5 7/8 ] ");
B.SetValues("[ 1/4 2/3 1/2; 4/3 10/5 7/5; 4/5 4/3 9/10 ] ");
A[1,1].SetFraction("4/13");
// check it worked
Console.Write("A = \"[ ");
for(int i = 0; i < 3 ; i++)
{
for(int j = 0; j < 3 ; j++)
{
Console.Write(A[i,j].ToString());
if (j < 2) Console.Write(" ");
}
if (i < 2) Console.Write("; ");
}
Console.WriteLine(" ] \"\n");
Console.Write("B = \"[ ");
for(int i = 0; i < 3 ; i++)
{
for(int j = 0; j < 3 ; j++)
{
Console.Write(B[i,j].ToString());
if (j < 2) Console.Write(" ");
}
if (i < 2) Console.Write("; ");
}
Console.WriteLine(" ] \"\n");
//Console.WriteLine("{0} + {1} = {2}\n\n\n", A, B, A + B);
//Console.WriteLine("{0} * {1} = {2}\n\n\n", A, B, A * B);
//Console.WriteLine("det(A) = {0} + det(B) = {1}", A.Det(), B.Det());
Console.Read();
}
}
}