1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
/**
* Returns a MathObject at the power n.
* @param n the power to apply to the MathObject.
* @param obj the MathObject to get the power.
* @return a MathObject at the power n.
**/
public static MathObject pow(int n, MathObject obj)
{
if(n == 0)
{
if(obj instanceof ComplexNumber)
{
ComplexNumber cplx = (ComplexNumber)obj;
return new ComplexNumber(1);
}
else if(obj instanceof Polynome)
return new Polynome(1);
else if(obj instanceof Matrix)
{
Matrix mat = (Matrix)obj;
return new Matrix(mat.getNbrRows(), Matrix.IDENTITY, null);
}
}
else if(n % 2 == 0)
obj = pow(2 , pow(n/2 , obj));
else
obj.multpiply(pow(2 , pow(n/2 , obj)));
return obj;
} |