/* Copyright 2011 Toby D. Rule
This file is part of CompPad, an OpenOffice extension to provide live
mathematical and engineering calculations within a Writer document.
CompPad is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CompPad is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CompPad. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* class Operators
*/
package com.CompPad.model;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.measure.unit.AlternateUnit;
import javax.measure.unit.ProductUnit;
import org.jscience.physics.amount.Amount;
import javax.measure.unit.Unit;
import org.jscience.mathematics.number.Complex;
/**
*
* @author Toby D. Rule
*/
public class Operators {
public static ComplexAmount pi(){
return ComplexAmount.valueOf(Math.PI,Unit.ONE);
}
public static ComplexAmount e(){
return ComplexAmount.valueOf(Math.E,Unit.ONE);
}
/* log base 10. These function names match the Amount functions */
public static ComplexAmount log10(ComplexAmount a){
ComplexAmount log10 = ComplexAmount.valueOf(10., Unit.ONE).log();
return a.log().divide(log10);
}
public static Matrix log10(Matrix A) throws Exception{
ComplexAmount log10 = ComplexAmount.valueOf(10., Unit.ONE).log();
return A.forEachElement("log").forEachElement("divide",log10,false);
}
/* natural logarith */
public static ComplexAmount log(ComplexAmount ca){
return ca.log();
}
public static Matrix log(Matrix A) throws Exception{
return A.forEachElement("log");
}
public static ComplexAmount sin(ComplexAmount a){
return a.sin();
}
public static ComplexAmount sin2(ComplexAmount a){
return a.sin().pow(ComplexAmount.valueOf(2.0,Unit.ONE));
}
public static Matrix sin(Matrix a) throws Exception{
return a.forEachElement("sin");
}
public static Matrix sin2(Matrix a) throws Exception{
return a.forEachElement(Operators.class,
Operators.class.getMethod("sin2", ComplexAmount.class));
}
public static ComplexAmount cos(ComplexAmount a){
return a.cos();
}
public static ComplexAmount cos2(ComplexAmount a){
return a.cos().pow(ComplexAmount.valueOf(2.0,Unit.ONE));
}
public static Matrix cos(Matrix a) throws Exception{
return a.forEachElement("cos");
}
public static Matrix cos2(Matrix a) throws Exception{
return a.forEachElement(Operators.class,
Operators.class.getMethod("cos2", ComplexAmount.class));
}
public static ComplexAmount tan(ComplexAmount a){
return a.tan();
}
public static ComplexAmount tan2(ComplexAmount a){
return a.tan().pow(ComplexAmount.valueOf(2.0,Unit.ONE));
}
public static Matrix tan(Matrix a) throws Exception{
return a.forEachElement("tan");
}
public static Matrix tan2(Matrix a) throws Exception{
return a.forEachElement(Operators.class,
Operators.class.getMethod("tan2", ComplexAmount.class));
}
public static ComplexAmount cot(ComplexAmount a){
return a.tan().inverse();
}
public static ComplexAmount cot2(ComplexAmount a){
return a.tan().inverse().pow(ComplexAmount.valueOf(2.0,Unit.ONE));
}
public static Matrix cot(Matrix A) throws Exception{
return A.forEachElement(Operators.class,
Operators.class.getMethod("cot",
ComplexAmount.class));
}
public static Matrix cot2(Matrix A) throws Exception{
return A.forEachElement(Operators.class,
Operators.class.getMethod("cot2",
ComplexAmount.class));
}
public static ComplexAmount sec(ComplexAmount a){
return a.cos().inverse();
}
public static ComplexAmount sec2(ComplexAmount a){
return a.cos().inverse().pow(ComplexAmount.valueOf(2.0,Unit.ONE));
}
public static Matrix sec(Matrix A) throws Exception{
return A.forEachElement(Operators.class,
Operators.class.getMethod("sec",
ComplexAmount.class));
}
public static Matrix sec2(Matrix A) throws Exception{
return A.forEachElement(Operators.class,
Operators.class.getMethod("sec2",
ComplexAmount.class));
}
public static ComplexAmount csc(ComplexAmount a){
return a.sin().inverse();
}
public static ComplexAmount csc2(ComplexAmount a){
return a.sin().inverse().pow(ComplexAmount.valueOf(2.0,Unit.ONE));
}
public static Matrix csc(Matrix A) throws Exception{
return A.forEachElement(Operators.class,
Operators.class.getMethod("csc",
ComplexAmount.class));
}
public static Matrix csc2(Matrix A) throws Exception{
return A.forEachElement(Operators.class,
Operators.class.getMethod("csc2",
ComplexAmount.class));
}
public static ComplexAmount asin(ComplexAmount a){
return a.asin();
}
public static Matrix asin(Matrix a) throws Exception{
return a.forEachElement("asin");
}
public static ComplexAmount acos(ComplexAmount a){
return a.acos();
}
public static Matrix acos(Matrix a) throws Exception{
return a.forEachElement("acos");
}
public static ComplexAmount atan(ComplexAmount a){
return a.atan();
}
public static Matrix atan(Matrix a) throws Exception{
return a.forEachElement("atan");
}
public static ComplexAmount acot(ComplexAmount a){
return a.inverse().atan();
}
public static Matrix acot(Matrix A) throws Exception{
return A.forEachElement(Operators.class,
Operators.class.getMethod("acot",
ComplexAmount.class));
}
public static ComplexAmount asec(ComplexAmount a){
return a.inverse().acos();
}
public static Matrix asec(Matrix A) throws Exception{
return A.forEachElement(Operators.class,
Operators.class.getMethod("asec",
ComplexAmount.class));
}
public static ComplexAmount acsc(ComplexAmount a){
return a.inverse().asin();
}
public static Matrix acsc(Matrix A) throws Exception{
return A.forEachElement(Operators.class,
Operators.class.getMethod("acsc",
ComplexAmount.class));
}
public static ComplexAmount pow(ComplexAmount b, ComplexAmount a){
return a.pow(b);
}
public static Matrix pow(ComplexAmount b, Matrix a){
return a.power(b);
}
public static Object pow(ElementWise b, ComplexAmount a) throws Exception{
Object bobj = b.get();
if (ComplexAmount.class.isInstance(bobj)){
return a.pow((ComplexAmount)bobj);
}
else if (Matrix.class.isInstance(bobj)){
return pow((Matrix)bobj, a);
}
else{
throw new IllegalArgumentException("Illegal arg2 for pow");
}
}
public static Object pow(ComplexAmount b, ElementWise a) throws Exception{
Object aobj = a.get();
if (ComplexAmount.class.isInstance(aobj)){
return ((ComplexAmount)aobj).pow(b);
}
else if (Matrix.class.isInstance(aobj)){
return ((Matrix)a.get()).forEachElement("pow", b, null);
}
else{
throw new IllegalArgumentException("Illegal arg1 for pow");
}
}
/* CompPad syntax requires that ElementWise operation be applied to one
* argument or th eother but not both */
// public static Object pow(ElementWise b, ElementWise a) throws Exception{
// Object aobj = a.get();
// Object bobj = b.get();
// if (Matrix.class.isInstance(bobj)){
// return pow((Matrix)bobj, a);
// }
// else if (Matrix.class.isInstance(aobj)){
// return pow(b, (Matrix)aobj);
// }
// else if ( ComplexAmount.class.isInstance(bobj)
// & ComplexAmount.class.isInstance(aobj)){
// return ((ComplexAmount)aobj).pow((ComplexAmount)bobj);
// }
// else{
// throw new IllegalArgumentException("Illegal arguments for pow");
// }
// }
public static Matrix pow(Matrix b, ElementWise a) throws Exception{
Object aobj = a.get();
if (ComplexAmount.class.isInstance(aobj)){
return b.forEachElement("pow", (ComplexAmount)aobj, true);
}
else if (Matrix.class.isInstance(aobj)){
return ((Matrix)a.get()).forEachElement("pow", b);
}
else{
throw new IllegalArgumentException("Illegal arg1 for pow");
}
}
public static Matrix pow(ElementWise b, Matrix a) throws Exception{
Object bobj = b.get();
if (ComplexAmount.class.isInstance(bobj)){
return a.forEachElement("pow", (ComplexAmount)bobj, null);
}
else if (Matrix.class.isInstance(bobj)){
return a.forEachElement("pow", (Matrix)bobj);
}
else{
throw new IllegalArgumentException("Illegal arg2 for pow");
}
}
public static Matrix pow(Matrix b, ComplexAmount a) throws Exception {
return b.forEachElement("pow", a, true);
}
public static ComplexAmount sqrt(ComplexAmount a){
return a.sqrt();
}
public static Object sqrt(ElementWise a) throws Exception{
Object aobj = a.get();
if (ComplexAmount.class.isInstance(aobj)){
return ((ComplexAmount)aobj).sqrt();
}
else if (Matrix.class.isInstance(aobj)){
return (((Matrix)a.get()).forEachElement("sqrt"));
}
else{
throw new IllegalArgumentException("Illegal argument for sqrt");
}
}
public static ComplexAmount nroot(ComplexAmount b, ComplexAmount a){
return b.pow(a.inverse());
}
public static Matrix nroot(ComplexAmount b, Matrix a) throws Exception{
return a.forEachElement("inverse").forEachElement("pow",b,true);
}
public static Object nroot(ElementWise b, ComplexAmount a) throws Exception{
Object bobj = b.get();
if (ComplexAmount.class.isInstance(bobj)){
return ((ComplexAmount)bobj).pow(a.inverse());
}
else if (Matrix.class.isInstance(bobj)){
return ((Matrix)bobj).forEachElement("pow",a.inverse(), false);
}
else{
throw new IllegalArgumentException("Illegal arguments for nroot");
}
}
// public static Matrix nroot(ElementWise b, ElementWise a) throws Exception {
// return ((Matrix)b.get()).forEachElement("pow",((Matrix)a.get()).forEachElement("inverse"));
// }
public static Matrix nroot(Matrix b, ElementWise a) throws Exception {
Object aobj = a.get();
if (ComplexAmount.class.isInstance(aobj)){
return (b).forEachElement("pow",((ComplexAmount)aobj).inverse(), false);
}
else if (Matrix.class.isInstance(aobj)){
return b.forEachElement("pow",((Matrix)a.get()).forEachElement("inverse"));
}
else{
throw new IllegalArgumentException("Illegal arguments to nroot");
}
}
public static Object nroot (ComplexAmount b, ElementWise a) throws Exception{
Object aobj = a.get();
if (ComplexAmount.class.isInstance(aobj)){
return b.pow(((ComplexAmount)aobj).inverse());
}
else if (Matrix.class.isInstance(aobj)){
return ((Matrix)aobj).forEachElement("inverse").forEachElement("pow",b,true);
}
else{
throw new IllegalArgumentException("Illegal arg1 for nroot");
}
}
public static Matrix nroot(ElementWise b, Matrix a) throws Exception {
Object bobj = b.get();
if (ComplexAmount.class.isInstance(bobj)){
return a.forEachElement("inverse").forEachElement("pow",(ComplexAmount)bobj,true);
}
else if (Matrix.class.isInstance(bobj)){
return ((Matrix)b.get()).forEachElement("pow",a.forEachElement("inverse"));
}
else{
throw new IllegalArgumentException("Illegal arguments for nroot");
}
}
public static Matrix nroot(Matrix b, ComplexAmount a){
throw new UnsupportedOperationException("n root of matrix not supported by Jscience library");
}
public static ComplexAmount add(ComplexAmount b, ComplexAmount a){
return b.plus(a);
}
public static Matrix add(ComplexAmount b, Matrix a){
return a.plus(b);
}
public static Matrix add(Matrix b, ComplexAmount a){
return b.plus(a);
}
public static Matrix add(Matrix b, Matrix a){
return a.plus(b);
}
public static ComplexAmount negative(ComplexAmount a){
return a.opposite();
}
public static ComplexAmount sub(ComplexAmount b, ComplexAmount a){
return a.minus(b);
}
public static Matrix sub(ComplexAmount b, Matrix a){
return a.minus(b);
}
public static Matrix sub(Matrix b, ComplexAmount a){
return b.times(ComplexAmount.valueOf(-1.0,Unit.ONE)).plus(a);
}
public static Matrix sub(Matrix b, Matrix a){
return b.minus(a);
}
public static ComplexAmount mult(ComplexAmount b, boolean a){
if (a){
return b;
}
else{
return ComplexAmount.valueOf(Amount.ZERO);
}
}
public static ComplexAmount mult(boolean b, ComplexAmount a){
return mult(a,b);
}
public static ComplexAmount mult(boolean b, boolean a){
if (a & b){
return ComplexAmount.valueOf(Amount.ONE);
}
else{
return ComplexAmount.valueOf(Amount.ZERO);
}
}
public static ComplexAmount mult(ComplexAmount b, ComplexAmount a){
return a.times(b);
}
public static Matrix mult(Matrix b, ComplexAmount a){
return b.times(a);
}
public static Matrix mult(ComplexAmount b, Matrix a){
return a.times(b);
}
public static Matrix mult(Matrix b, Matrix a){
return a.times(b);
}
public static Object mult(ComplexAmount b, ElementWise a){
if (ComplexAmount.class.isInstance(a.get())){
return Operators.mult(b, (ComplexAmount)a.get());
}
else if (Matrix.class.isInstance(a.get())){
return Operators.mult(b,(Matrix)a.get());
}
else{
throw new IllegalArgumentException("Bad arguments to Operators.mult");
}
}
public static Object mult(ElementWise b, ComplexAmount a){
Object bobj = b.get();
if (ComplexAmount.class.isInstance(bobj)){
return Operators.mult(a, (ComplexAmount)bobj);
}
else if (Matrix.class.isInstance(bobj)){
return Operators.mult(a,(Matrix)bobj);
}
else{
throw new IllegalArgumentException("Bad arguments to Operators.mult");
}
}
/* Comppad syntax requires elementwise operation be applied to
* one argument or tghe other, but not both */
// public static Matrix mult(ElementWise b, ElementWise a) throws Exception{
// return ((Matrix)a.get()).forEachElement("times", (Matrix)b.get());
// }
public static Matrix mult(Matrix b, ElementWise a) throws Exception{
Object aobj = a.get();
if ( ComplexAmount.class.isInstance( a.get())){
return mult(b,(ComplexAmount)aobj);
}
else{
return ((Matrix)a.get()).forEachElement("times", b);
}
}
public static Matrix mult(ElementWise b, Matrix a) throws Exception{
Object bobj = b.get();
if (ComplexAmount.class.isInstance(bobj)){
return mult((ComplexAmount)bobj, a);
}
else{
return a.forEachElement("times", (Matrix)b.get());
}
}
public static ComplexAmount div(ComplexAmount b, ComplexAmount a){
return a.divide(b);
}
public static Object div(ComplexAmount b, ElementWise a) throws Exception{
if (ComplexAmount.class.isInstance(a.get())){
return Operators.div(b, (ComplexAmount)a.get());
}
else if (Matrix.class.isInstance(a.get())){
return Operators.div(b,(Matrix)a.get());
}
else{
throw new IllegalArgumentException("Bad arguments to Operators.div");
}
}
public static Matrix div(ComplexAmount b, Matrix A) throws Exception{
return A.forEachElement("divide", b,null);
}
public static Matrix div(Matrix B, ComplexAmount a) throws Exception{
return B.inverse().forEachElement("times",a,null);
}
/* Comppad syntax allows elementwise operator to be applied to
* one argument or the other, but not both */
// public static Matrix div(ElementWise b, ElementWise a) throws Exception{
// return ((Matrix)a.get()).forEachElement("divide", (Matrix)b.get());
// }
public static Matrix div(Matrix b, ElementWise a) throws Exception{
Object aobj = a.get();
if (ComplexAmount.class.isInstance(aobj)){
return b.forEachElement("divide", (ComplexAmount)aobj,true);
}
else if (Matrix.class.isInstance(aobj)){
return ((Matrix)a.get()).forEachElement("divide", b);
}
else{
throw new IllegalArgumentException("Illegal arg1 for div");
}
}
public static Matrix div(ElementWise b, Matrix a) throws Exception{
Object bobj = b.get();
if (ComplexAmount.class.isInstance(bobj)){
return a.forEachElement("divide", (ComplexAmount)bobj,false);
}
else if (Matrix.class.isInstance(bobj)){
return a.forEachElement("divide", (Matrix)bobj);
}
else{
throw new IllegalArgumentException("Illegal arg2 for div");
}
}
public static Object div(ElementWise b, ComplexAmount a) throws Exception{
Object bobj = b.get();
if (ComplexAmount.class.isInstance(bobj)){
return a.div((ComplexAmount)bobj);
}
else if(Matrix.class.isInstance(bobj)){
return ((Matrix)b.get()).forEachElement("div",a,true);
}
else{
throw new IllegalArgumentException("Illegal arg 2 for div");
}
}
public static Matrix div (Matrix B, Matrix A){
return A.div(B);
}
public static ComplexAmount abs(ComplexAmount a){
return a.abs();
}
public static boolean and(boolean s, boolean t){
return (s & t);
}
public static Matrix and(Matrix B,Matrix A) throws Exception {
return A.forEachElement(Operators.class,
Operators.class.getMethod("and",
boolean.class, boolean.class), B);
}
public static Matrix and(Matrix B, boolean a) throws Exception {
return B.forEachElement(Operators.class,
Operators.class.getMethod("and",
boolean.class,boolean.class),a, true);
}
public static Matrix and(boolean b, Matrix A) throws Exception {
return Operators.and(A, b);
}
public static boolean or(boolean s, boolean t){
return (s | t);
}
public static Matrix or(Matrix B,Matrix A) throws Exception {
return A.forEachElement(Operators.class,
Operators.class.getMethod("or",
boolean.class, boolean.class), B);
}
public static Matrix or(Matrix B, boolean a) throws Exception {
return B.forEachElement(Operators.class,
Operators.class.getMethod("or",
boolean.class,boolean.class),a,true);
}
public static Matrix or(boolean b, Matrix A) throws Exception {
return Operators.or(A, b);
}
public static boolean eq(Object a, Object b){
if (ComplexAmount.class.isInstance(a)
& ComplexAmount.class.isInstance(b)){
return a.equals(b);
}
else if (Boolean.class.isInstance(a) & Boolean.class.isInstance(b)){
return a==b;
}
else{
throw new IllegalArgumentException("eq: Illegal Argument");
}
}
public static Matrix eq(Matrix B,Matrix A) throws Exception {
return A.forEachElement(Operators.class,
Operators.class.getMethod("eq",
Object.class, Object.class), B);
}
public static Matrix eq(Matrix B, Object a) throws Exception {
return B.forEachElement(Operators.class,
Operators.class.getMethod("eq",
Object.class,Object.class),a,true);
}
public static Matrix eq(Object b, Matrix A) throws Exception {
return Operators.eq(A, b);
}
public static boolean neq (Object b, Object a){
if (ComplexAmount.class.isInstance(a)
& ComplexAmount.class.isInstance(b)){
return !a.equals(b);
}
else if (Boolean.class.isInstance(a) & Boolean.class.isInstance(b)){
return a != b;
}
else{
throw new IllegalArgumentException("neq: Illegal Arguments");
}
}
public static Matrix neq(Matrix B,Matrix A) throws Exception {
return A.forEachElement(Operators.class,
Operators.class.getMethod("neq",
Object.class, Object.class), B);
}
public static Matrix neq(Matrix B, Object a) throws Exception {
return B.forEachElement(Operators.class,
Operators.class.getMethod("neq",
Object.class,Object.class),a,true);
}
public static Matrix neq(Object b, Matrix A) throws Exception {
return Operators.neq(A, b);
}
public static boolean not(boolean a){
return !a;
}
public static Matrix not(Matrix A) throws Exception {
return A.forEachElement(Operators.class,
Operators.class.getMethod("not", boolean.class));
}
public static boolean lte (Object b, Object a){
if (ComplexAmount.class.isInstance(a)
& ComplexAmount.class.isInstance(b)){
return ((ComplexAmount)a).lt((ComplexAmount) b) ||
a.equals(b);
}
else{
throw new IllegalArgumentException("lte: Illegal Arguments");
}
}
public static Matrix lte(Matrix B,Matrix A) throws Exception {
return A.forEachElement(Operators.class,
Operators.class.getMethod("lte",
Object.class, Object.class), B);
}
public static Matrix lte(Matrix B, Object a) throws Exception {
return B.forEachElement(Operators.class,
Operators.class.getMethod("lte",
Object.class,Object.class),a,true);
}
public static Matrix lte(Object b, Matrix A) throws Exception {
return Operators.gte( A, b);
}
public static boolean gte (Object b, Object a){
if (ComplexAmount.class.isInstance(a)
& ComplexAmount.class.isInstance(b)){
return ((ComplexAmount)a).gt((ComplexAmount) b) ||
a.equals(b);
}
else{
throw new IllegalArgumentException("gte: Illegal Arguments");
}
}
public static Matrix gte(Matrix B,Matrix A) throws Exception {
return A.forEachElement(Operators.class,
Operators.class.getMethod("gte",
Object.class, Object.class), B);
}
public static Matrix gte(Matrix B, Object a) throws Exception {
return B.forEachElement(Operators.class,
Operators.class.getMethod("gte",
Object.class,Object.class),a,true);
}
public static Matrix gte(Object b, Matrix A) throws Exception {
return Operators.lte( A, b);
}
public static boolean lt (Object b, Object a){
if (ComplexAmount.class.isInstance(a)
& ComplexAmount.class.isInstance(b)){
return ((ComplexAmount)a).lt((ComplexAmount) b);
}
else{
throw new IllegalArgumentException("lt: Illegal Arguments");
}
}
public static Matrix lt(Matrix B,Matrix A) throws Exception {
return A.forEachElement(Operators.class,
Operators.class.getMethod("lt",
Object.class, Object.class), B);
}
public static Matrix lt(Matrix B, Object a) throws Exception {
return B.forEachElement(Operators.class,
Operators.class.getMethod("lt",
Object.class,Object.class),a,true);
}
public static Matrix lt(Object b, Matrix A) throws Exception {
return Operators.gt( A, b);
}
public static boolean gt(ComplexAmount b, ComplexAmount a){
return a.gt(b);
}
public static boolean gt (Object b, Object a){
if (ComplexAmount.class.isInstance(a)
& ComplexAmount.class.isInstance(b)){
return ((ComplexAmount)a).gt((ComplexAmount) b);
}
else{
throw new IllegalArgumentException("gte: Illegal Arguments");
}
}
public static Matrix gt(Matrix B,Matrix A) throws Exception {
return A.forEachElement(Operators.class,
Operators.class.getMethod("gt",
Object.class, Object.class), B);
}
public static Matrix gt(Matrix B, Object a) throws Exception {
return B.forEachElement(Operators.class,
Operators.class.getMethod("gt",
Object.class,Object.class),a,true);
}
public static Matrix gt(Object b, Matrix A) throws Exception {
return Operators.lt( A, b);
}
/* This method splits up units that are a product of other units into their
* base units. */
/* This method should return units either in SI, NonSI, or combinations of
* units acording to the Unit.valueOf format. */
private static javax.measure.unit.Unit splitUnits(javax.measure.unit.Unit u){
Unit retval=Unit.ONE;
u=u.getStandardUnit();
/* If it's a ProductUnit, then split it and then call splitUnits */
if (javax.measure.unit.ProductUnit.class.isInstance(u)){
ProductUnit pu=(ProductUnit)u;
for (int i=0;i<pu.getUnitCount();++i){
/* First check to see if this unit needs split */
Unit ux =splitUnits(pu.getUnit(i));
retval=retval.times(ux.pow(pu.getUnitPow(i)));
}
/* Now split again */
Logger.getLogger("com.CompPad").log(Level.FINE,"ProductUnit "
+ pu.getUnitCount());
}
else if (javax.measure.unit.AlternateUnit.class.isInstance(u)){
retval=((AlternateUnit)u).getParent();
/* Now split again */
Logger.getLogger("com.CompPad").log(Level.FINE,
"AlternateUnit "+retval );
retval=splitUnits(retval);
}
else{
retval=u;
/* Must be base units */
}
return retval;
}
public static ComplexAmount round(ComplexAmount u, ComplexAmount x){
ComplexAmount x1 = x.divide(u);
Double x1r = ((Float) ((Long) Math.round(x1.getReal(Unit.ONE))).floatValue()).doubleValue();
Double x1i = ((Float) ((Long) Math.round(x1.getImaginary(Unit.ONE))).floatValue()).doubleValue();
ComplexAmount retval = ComplexAmount.valueOf(
Complex.valueOf(x1r,x1i),Unit.ONE);
return retval.times(u);
}
public static Matrix round(ComplexAmount u, Matrix A) throws Exception{
return A.forEachElement(Operators.class,
Operators.class.getMethod("round",
ComplexAmount.class, ComplexAmount.class), u,true);
}
public static Matrix round(Matrix U, ComplexAmount a) throws Exception{
return U.forEachElement(Operators.class,
Operators.class.getMethod("round",
ComplexAmount.class, ComplexAmount.class), a,false);
}
public static Matrix round(Matrix U, Matrix A) throws Exception{
return U.forEachElement(Operators.class,
Operators.class.getMethod("round",
ComplexAmount.class, ComplexAmount.class), A);
}
public static ComplexAmount ceil(ComplexAmount u, ComplexAmount x){
ComplexAmount x1 = x.divide(u);
Double x1r = Math.ceil(x1.getReal(Unit.ONE));
Double x1i = Math.ceil(x1.getImaginary(Unit.ONE));
ComplexAmount retval = ComplexAmount.valueOf(
Complex.valueOf(x1r,x1i),Unit.ONE);
return retval.times(u);
}
public static Matrix ceil(ComplexAmount u, Matrix A) throws Exception{
return A.forEachElement(Operators.class,
Operators.class.getMethod("ceil",
ComplexAmount.class, ComplexAmount.class), u,true);
}
public static Matrix ceil(Matrix U, ComplexAmount a) throws Exception{
return U.forEachElement(Operators.class,
Operators.class.getMethod("ceil",
ComplexAmount.class, ComplexAmount.class), a,false);
}
public static Matrix ceil(Matrix U, Matrix A) throws Exception{
return U.forEachElement(Operators.class,
Operators.class.getMethod("ceil",
ComplexAmount.class, ComplexAmount.class), A);
}
public static ComplexAmount floor(ComplexAmount u, ComplexAmount x){
ComplexAmount x1 = x.divide(u);
Double x1r = Math.floor(x1.getReal(Unit.ONE));
Double x1i = Math.floor(x1.getImaginary(Unit.ONE));
ComplexAmount retval = ComplexAmount.valueOf(
Complex.valueOf(x1r,x1i),Unit.ONE);
return retval.times(u);
}
public static Matrix floor(ComplexAmount u, Matrix A) throws Exception{
return A.forEachElement(Operators.class,
Operators.class.getMethod("floor",
ComplexAmount.class, ComplexAmount.class), u,true);
}
public static Matrix floor(Matrix U, ComplexAmount a) throws Exception{
return U.forEachElement(Operators.class,
Operators.class.getMethod("floor",
ComplexAmount.class, ComplexAmount.class), a,false);
}
public static Matrix floor(Matrix U, Matrix A) throws Exception{
return U.forEachElement(Operators.class,
Operators.class.getMethod("floor",
ComplexAmount.class, ComplexAmount.class), A);
}
/* Rand, zeros and ones are basically the same function - I should be able to
* re-use most of this code, but it is easier to just cut and past! */
public static Object rand(ComplexAmount cn, ComplexAmount cm){
int m;
int n;
Object retval = null;
if (cn.isInteger() & cm.isInteger()
& cn.getUnit().isCompatible(Unit.ONE)
& cm.getUnit().isCompatible(Unit.ONE)){
m = Operators.round(ComplexAmount.valueOf(Amount.ONE),cm).getReal(Unit.ONE).intValue();
n = Operators.round(ComplexAmount.valueOf(Amount.ONE),cn).getReal(Unit.ONE).intValue();
}
else{
throw (new IllegalArgumentException("arguments must be integer"));
}
if (m == 0 & n == 0){
retval = ComplexAmount.valueOf(Complex.valueOf(Math.random(), 0) , Unit.ONE);
}
else{
for (int i=0; i< m; ++i){
Matrix row = Matrix.valueOf(ComplexAmount.valueOf(Complex.valueOf(Math.random(),0),Unit.ONE));
for (int j=1; j<n; ++j){
row = row.addColumn(ComplexAmount.valueOf(Complex.valueOf(Math.random(),0),Unit.ONE));
}
if (i == 0){
retval = row;
}
else{
retval = ((Matrix)retval).addRow(row);
}
}
}
return retval;
}
public static Object zeros(ComplexAmount cn, ComplexAmount cm){
int m;
int n;
Object retval = null;
if (cn.isInteger() & cm.isInteger()
& cn.getUnit().isCompatible(Unit.ONE)
& cm.getUnit().isCompatible(Unit.ONE)){
m = Operators.round(ComplexAmount.valueOf(Amount.ONE),cm).getReal(Unit.ONE).intValue();
n = Operators.round(ComplexAmount.valueOf(Amount.ONE),cn).getReal(Unit.ONE).intValue();
}
else{
throw (new IllegalArgumentException("arguments must be integer"));
}
if (m == 0 & n == 0){
retval = ComplexAmount.valueOf(Amount.ZERO);
}
else{
for (int i=0; i< m; ++i){
Matrix row = Matrix.valueOf(ComplexAmount.valueOf(Amount.ZERO));
for (int j=1; j<n; ++j){
row = row.addColumn(ComplexAmount.valueOf(Amount.ZERO));
}
if (i == 0){
retval = row;
}
else{
retval = ((Matrix)retval).addRow(row);
}
}
}
return retval;
}
public static Object ones(ComplexAmount cn, ComplexAmount cm){
int m;
int n;
Object retval = null;
if (cn.isInteger() & cm.isInteger()
& cn.getUnit().isCompatible(Unit.ONE)
& cm.getUnit().isCompatible(Unit.ONE)){
m = Operators.round(ComplexAmount.valueOf(Amount.ONE),cm).getReal(Unit.ONE).intValue();
n = Operators.round(ComplexAmount.valueOf(Amount.ONE),cn).getReal(Unit.ONE).intValue();
}
else{
throw (new IllegalArgumentException("arguments must be integer"));
}
if (m == 0 & n == 0){
retval = ComplexAmount.valueOf(Amount.ONE);
}
else{
for (int i=0; i< m; ++i){
Matrix row = Matrix.valueOf(ComplexAmount.valueOf(Amount.ONE));
for (int j=1; j<n; ++j){
row = row.addColumn(ComplexAmount.valueOf(Amount.ONE));
}
if (i == 0){
retval = row;
}
else{
retval = ((Matrix)retval).addRow(row);
}
}
}
return retval;
}
public static ComplexAmount real (ComplexAmount x){
return x.getReal();
}
public static Matrix real(Matrix A) throws java.lang.Exception{
return A.forEachElement(Operators.class,
Operators.class.getMethod("real", ComplexAmount.class));
}
public static ComplexAmount imaginary (ComplexAmount x){
return x.getImaginary();
}
public static Matrix imaginary(Matrix A) throws java.lang.Exception{
return A.forEachElement(Operators.class,
Operators.class.getMethod("imaginary",ComplexAmount.class));
}
public static ComplexAmount argument (ComplexAmount x){
return x.argument();
}
public static Matrix argument (Matrix A) throws java.lang.Exception{
return A.forEachElement(Operators.class,
Operators.class.getMethod("argument", ComplexAmount.class));
}
public static ComplexAmount signum (ComplexAmount x){
return x.signum();
}
public static Matrix signum (Matrix A) throws java.lang.Exception{
return A.forEachElement(Operators.class,
Operators.class.getMethod("signum", ComplexAmount.class));
}
public static ComplexAmount conjugate (ComplexAmount x){
return x.conjugate();
}
public static Matrix conjugate (Matrix A) throws java.lang.Exception{
return A.forEachElement(Operators.class,
Operators.class.getMethod("argument", ComplexAmount.class));
}
/* gets units from SI or nonSI class and split into base units */
public static ComplexAmount units(String s)
throws IllegalArgumentException, IllegalAccessException {
javax.measure.unit.Unit myObject;
try{
myObject = (Unit)javax.measure.unit.SI.class.getField(s).get(null);
}
catch(NoSuchFieldException e){
try{
myObject=(Unit)javax.measure.unit.NonSI.class.getField(s).get(null);
}
catch(NoSuchFieldException ee){
myObject=javax.measure.unit.Unit.valueOf(s);
}
}
Unit old=myObject;
// create set of split units
myObject=splitUnits(myObject);
// .to method does getStandardUnit
// convert old units to split units
return ComplexAmount.valueOf(Amount.valueOf(1.0, old).to(myObject));
}
public static ComplexAmount literal(double a){
return ComplexAmount.valueOf(Amount.valueOf(a,Unit.ONE));
}
public static Matrix transpose(Matrix a){
return a.transpose();
}
public static ComplexAmount sum(Matrix A){
return A.sum();
}
public static ComplexAmount prod(Matrix A){
return A.prod();
}
public static ComplexAmount max(Matrix A){
return A.max();
}
public static ComplexAmount min(Matrix A){
return A.min();
}
public static ComplexAmount rows(Matrix A){
return A.getNumberOfRows();
}
public static ComplexAmount columns (Matrix A){
return A.getNumberOfColumns();
}
public static Matrix size(Matrix A){
/* Return row-vector with matrix dimensions */
return Matrix.valueOf(A.getNumberOfRows()).addColumn(A.getNumberOfColumns());
}
public static Matrix range(ComplexAmount b, ComplexAmount a){
boolean isIntegers = a.isInteger()& b.isInteger();
int aa = (int)Math.round(a.getReal(Unit.ONE));
int bb = (int)Math.round(b.getReal(Unit.ONE));
Matrix retval = null;
Integer dir;
if (bb>aa){
dir = 1;
}
else{
dir = -1;
}
if (isIntegers){
retval = Matrix.valueOf(ComplexAmount.valueOf(
((Integer)aa).doubleValue(),Unit.ONE) );
for (Integer i = aa*dir + 1; i <= bb*dir; ++i){
/* Maybe we should have a way to perform these operations in-place? */
retval = retval.addRow(Matrix.valueOf(ComplexAmount.valueOf(i.doubleValue()
* dir.doubleValue(),Unit.ONE)));
}
return retval;
}
else{
throw (new java.lang.IllegalArgumentException("Range arguments must be integer"));
}
}
public static void setSigFigs(ComplexAmount a){
if (a.isInteger()){
Utility.numSigFigs= Math.round(a.getReal(Unit.ONE).floatValue());
}
else{
throw new IllegalArgumentException("argument must be integer");
}
}
public static Matrix subscript(Matrix b, Matrix a){
return (Matrix) a.get(b);
}
public static Object subscript(ComplexAmount b, Matrix a){
return a.get(b.getReal(Unit.ONE).intValue());
}
public static Matrix addColumn(Matrix b, Matrix a){
return a.addColumn(b);
}
public static Matrix addColumn(Object b, Matrix a){
return a.addColumn(b);
}
public static Matrix addColumn(Matrix b, Object a){
return Matrix.valueOf(a).addColumn(b);
}
public static Matrix addColumn(Object b, Object a){
return Matrix.valueOf(a).addColumn(b);
}
public static Matrix addRow(Matrix b, Matrix a){
return a.addRow(b);
}
public static Matrix addRow(Object b, Matrix a){
return a.addRow(Matrix.valueOf(b));
}
public static Matrix addRow(Matrix b, Object a){
return Matrix.valueOf(a).addRow(b);
}
public static Matrix addRow(Object b, Object a){
return Matrix.valueOf(a).addRow(Matrix.valueOf(b));
}
public static Plot plotxy(Matrix A){
Plot retval = new Plot(A);
return retval;
}
public static ElementWise elwise(Matrix o){
return new ElementWise(o);
}
public static ElementWise elwise (ComplexAmount o){
return new ElementWise (o);
}
}