MATH-795: factored out testMapFunctions.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1344989 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastien Brisard 2012-06-01 05:31:47 +00:00
parent 47635bdb4a
commit 06c509b3b8
3 changed files with 368 additions and 685 deletions

View File

@ -59,9 +59,6 @@ import org.junit.Test;
* @version $Id$
*/
public class ArrayRealVectorTest extends RealVectorAbstractTest {
// tolerances
protected double entryTolerance = 10E-16;
protected double normTolerance = 10E-14;
// Testclass to test the RealVector interface
// only with enough content to support the test
@ -511,334 +508,6 @@ public class ArrayRealVectorTest extends RealVectorAbstractTest {
Assert.assertNotSame("testData not same object ", v1.getDataRef(), a_double);
}
@Test
public void testMapFunctions() {
ArrayRealVector v1 = new ArrayRealVector(vec1);
//octave = v1 .+ 2.0
RealVector v_mapAdd = v1.mapAdd(2.0d);
double[] result_mapAdd = {3d, 4d, 5d};
assertClose("compare vectors" ,result_mapAdd,v_mapAdd.toArray(),normTolerance);
//octave = v1 .+ 2.0
RealVector v_mapAddToSelf = v1.copy();
v_mapAddToSelf.mapAddToSelf(2.0d);
double[] result_mapAddToSelf = {3d, 4d, 5d};
assertClose("compare vectors" ,result_mapAddToSelf,v_mapAddToSelf.toArray(),normTolerance);
//octave = v1 .- 2.0
RealVector v_mapSubtract = v1.mapSubtract(2.0d);
double[] result_mapSubtract = {-1d, 0d, 1d};
assertClose("compare vectors" ,result_mapSubtract,v_mapSubtract.toArray(),normTolerance);
//octave = v1 .- 2.0
RealVector v_mapSubtractToSelf = v1.copy();
v_mapSubtractToSelf.mapSubtractToSelf(2.0d);
double[] result_mapSubtractToSelf = {-1d, 0d, 1d};
assertClose("compare vectors" ,result_mapSubtractToSelf,v_mapSubtractToSelf.toArray(),normTolerance);
//octave = v1 .* 2.0
RealVector v_mapMultiply = v1.mapMultiply(2.0d);
double[] result_mapMultiply = {2d, 4d, 6d};
assertClose("compare vectors" ,result_mapMultiply,v_mapMultiply.toArray(),normTolerance);
//octave = v1 .* 2.0
RealVector v_mapMultiplyToSelf = v1.copy();
v_mapMultiplyToSelf.mapMultiplyToSelf(2.0d);
double[] result_mapMultiplyToSelf = {2d, 4d, 6d};
assertClose("compare vectors" ,result_mapMultiplyToSelf,v_mapMultiplyToSelf.toArray(),normTolerance);
//octave = v1 ./ 2.0
RealVector v_mapDivide = v1.mapDivide(2.0d);
double[] result_mapDivide = {.5d, 1d, 1.5d};
assertClose("compare vectors" ,result_mapDivide,v_mapDivide.toArray(),normTolerance);
//octave = v1 ./ 2.0
RealVector v_mapDivideToSelf = v1.copy();
v_mapDivideToSelf.mapDivideToSelf(2.0d);
double[] result_mapDivideToSelf = {.5d, 1d, 1.5d};
assertClose("compare vectors" ,result_mapDivideToSelf,v_mapDivideToSelf.toArray(),normTolerance);
//octave = v1 .^ 2.0
RealVector v_mapPow = v1.map(new Power(2));
double[] result_mapPow = {1d, 4d, 9d};
assertClose("compare vectors" ,result_mapPow,v_mapPow.toArray(),normTolerance);
//octave = v1 .^ 2.0
RealVector v_mapPowToSelf = v1.copy();
v_mapPowToSelf.mapToSelf(new Power(2));
double[] result_mapPowToSelf = {1d, 4d, 9d};
assertClose("compare vectors" ,result_mapPowToSelf,v_mapPowToSelf.toArray(),normTolerance);
//octave = exp(v1)
RealVector v_mapExp = v1.map(new Exp());
double[] result_mapExp = {2.718281828459045e+00d,7.389056098930650e+00d, 2.008553692318767e+01d};
assertClose("compare vectors" ,result_mapExp,v_mapExp.toArray(),normTolerance);
//octave = exp(v1)
RealVector v_mapExpToSelf = v1.copy();
v_mapExpToSelf.mapToSelf(new Exp());
double[] result_mapExpToSelf = {2.718281828459045e+00d,7.389056098930650e+00d, 2.008553692318767e+01d};
assertClose("compare vectors" ,result_mapExpToSelf,v_mapExpToSelf.toArray(),normTolerance);
//octave = ???
RealVector v_mapExpm1 = v1.map(new Expm1());
double[] result_mapExpm1 = {1.718281828459045d,6.38905609893065d, 19.085536923187668d};
assertClose("compare vectors" ,result_mapExpm1,v_mapExpm1.toArray(),normTolerance);
//octave = ???
RealVector v_mapExpm1ToSelf = v1.copy();
v_mapExpm1ToSelf.mapToSelf(new Expm1());
double[] result_mapExpm1ToSelf = {1.718281828459045d,6.38905609893065d, 19.085536923187668d};
assertClose("compare vectors" ,result_mapExpm1ToSelf,v_mapExpm1ToSelf.toArray(),normTolerance);
//octave = log(v1)
RealVector v_mapLog = v1.map(new Log());
double[] result_mapLog = {0d,6.931471805599453e-01d, 1.098612288668110e+00d};
assertClose("compare vectors" ,result_mapLog,v_mapLog.toArray(),normTolerance);
//octave = log(v1)
RealVector v_mapLogToSelf = v1.copy();
v_mapLogToSelf.mapToSelf(new Log());
double[] result_mapLogToSelf = {0d,6.931471805599453e-01d, 1.098612288668110e+00d};
assertClose("compare vectors" ,result_mapLogToSelf,v_mapLogToSelf.toArray(),normTolerance);
//octave = log10(v1)
RealVector v_mapLog10 = v1.map(new Log10());
double[] result_mapLog10 = {0d,3.010299956639812e-01d, 4.771212547196624e-01d};
assertClose("compare vectors" ,result_mapLog10,v_mapLog10.toArray(),normTolerance);
//octave = log(v1)
RealVector v_mapLog10ToSelf = v1.copy();
v_mapLog10ToSelf.mapToSelf(new Log10());
double[] result_mapLog10ToSelf = {0d,3.010299956639812e-01d, 4.771212547196624e-01d};
assertClose("compare vectors" ,result_mapLog10ToSelf,v_mapLog10ToSelf.toArray(),normTolerance);
//octave = ???
RealVector v_mapLog1p = v1.map(new Log1p());
double[] result_mapLog1p = {0.6931471805599453d,1.0986122886681096d,1.3862943611198906d};
assertClose("compare vectors" ,result_mapLog1p,v_mapLog1p.toArray(),normTolerance);
//octave = ???
RealVector v_mapLog1pToSelf = v1.copy();
v_mapLog1pToSelf.mapToSelf(new Log1p());
double[] result_mapLog1pToSelf = {0.6931471805599453d,1.0986122886681096d,1.3862943611198906d};
assertClose("compare vectors" ,result_mapLog1pToSelf,v_mapLog1pToSelf.toArray(),normTolerance);
//octave = cosh(v1)
RealVector v_mapCosh = v1.map(new Cosh());
double[] result_mapCosh = {1.543080634815244e+00d,3.762195691083631e+00d, 1.006766199577777e+01d};
assertClose("compare vectors" ,result_mapCosh,v_mapCosh.toArray(),normTolerance);
//octave = cosh(v1)
RealVector v_mapCoshToSelf = v1.copy();
v_mapCoshToSelf.mapToSelf(new Cosh());
double[] result_mapCoshToSelf = {1.543080634815244e+00d,3.762195691083631e+00d, 1.006766199577777e+01d};
assertClose("compare vectors" ,result_mapCoshToSelf,v_mapCoshToSelf.toArray(),normTolerance);
//octave = sinh(v1)
RealVector v_mapSinh = v1.map(new Sinh());
double[] result_mapSinh = {1.175201193643801e+00d,3.626860407847019e+00d, 1.001787492740990e+01d};
assertClose("compare vectors" ,result_mapSinh,v_mapSinh.toArray(),normTolerance);
//octave = sinh(v1)
RealVector v_mapSinhToSelf = v1.copy();
v_mapSinhToSelf.mapToSelf(new Sinh());
double[] result_mapSinhToSelf = {1.175201193643801e+00d,3.626860407847019e+00d, 1.001787492740990e+01d};
assertClose("compare vectors" ,result_mapSinhToSelf,v_mapSinhToSelf.toArray(),normTolerance);
//octave = tanh(v1)
RealVector v_mapTanh = v1.map(new Tanh());
double[] result_mapTanh = {7.615941559557649e-01d,9.640275800758169e-01d,9.950547536867305e-01d};
assertClose("compare vectors" ,result_mapTanh,v_mapTanh.toArray(),normTolerance);
//octave = tanh(v1)
RealVector v_mapTanhToSelf = v1.copy();
v_mapTanhToSelf.mapToSelf(new Tanh());
double[] result_mapTanhToSelf = {7.615941559557649e-01d,9.640275800758169e-01d,9.950547536867305e-01d};
assertClose("compare vectors" ,result_mapTanhToSelf,v_mapTanhToSelf.toArray(),normTolerance);
//octave = cos(v1)
RealVector v_mapCos = v1.map(new Cos());
double[] result_mapCos = {5.403023058681398e-01d,-4.161468365471424e-01d, -9.899924966004454e-01d};
assertClose("compare vectors" ,result_mapCos,v_mapCos.toArray(),normTolerance);
//octave = cos(v1)
RealVector v_mapCosToSelf = v1.copy();
v_mapCosToSelf.mapToSelf(new Cos());
double[] result_mapCosToSelf = {5.403023058681398e-01d,-4.161468365471424e-01d, -9.899924966004454e-01d};
assertClose("compare vectors" ,result_mapCosToSelf,v_mapCosToSelf.toArray(),normTolerance);
//octave = sin(v1)
RealVector v_mapSin = v1.map(new Sin());
double[] result_mapSin = {8.414709848078965e-01d,9.092974268256817e-01d,1.411200080598672e-01d};
assertClose("compare vectors" ,result_mapSin,v_mapSin.toArray(),normTolerance);
//octave = sin(v1)
RealVector v_mapSinToSelf = v1.copy();
v_mapSinToSelf.mapToSelf(new Sin());
double[] result_mapSinToSelf = {8.414709848078965e-01d,9.092974268256817e-01d,1.411200080598672e-01d};
assertClose("compare vectors" ,result_mapSinToSelf,v_mapSinToSelf.toArray(),normTolerance);
//octave = tan(v1)
RealVector v_mapTan = v1.map(new Tan());
double[] result_mapTan = {1.557407724654902e+00d,-2.185039863261519e+00d,-1.425465430742778e-01d};
assertClose("compare vectors" ,result_mapTan,v_mapTan.toArray(),normTolerance);
//octave = tan(v1)
RealVector v_mapTanToSelf = v1.copy();
v_mapTanToSelf.mapToSelf(new Tan());
double[] result_mapTanToSelf = {1.557407724654902e+00d,-2.185039863261519e+00d,-1.425465430742778e-01d};
assertClose("compare vectors" ,result_mapTanToSelf,v_mapTanToSelf.toArray(),normTolerance);
double[] vat_a = {0d, 0.5d, 1.0d};
ArrayRealVector vat = new ArrayRealVector(vat_a);
//octave = acos(vat)
RealVector v_mapAcos = vat.map(new Acos());
double[] result_mapAcos = {1.570796326794897e+00d,1.047197551196598e+00d, 0.0d};
assertClose("compare vectors" ,result_mapAcos,v_mapAcos.toArray(),normTolerance);
//octave = acos(vat)
RealVector v_mapAcosToSelf = vat.copy();
v_mapAcosToSelf.mapToSelf(new Acos());
double[] result_mapAcosToSelf = {1.570796326794897e+00d,1.047197551196598e+00d, 0.0d};
assertClose("compare vectors" ,result_mapAcosToSelf,v_mapAcosToSelf.toArray(),normTolerance);
//octave = asin(vat)
RealVector v_mapAsin = vat.map(new Asin());
double[] result_mapAsin = {0.0d,5.235987755982989e-01d,1.570796326794897e+00d};
assertClose("compare vectors" ,result_mapAsin,v_mapAsin.toArray(),normTolerance);
//octave = asin(vat)
RealVector v_mapAsinToSelf = vat.copy();
v_mapAsinToSelf.mapToSelf(new Asin());
double[] result_mapAsinToSelf = {0.0d,5.235987755982989e-01d,1.570796326794897e+00d};
assertClose("compare vectors" ,result_mapAsinToSelf,v_mapAsinToSelf.toArray(),normTolerance);
//octave = atan(vat)
RealVector v_mapAtan = vat.map(new Atan());
double[] result_mapAtan = {0.0d,4.636476090008061e-01d,7.853981633974483e-01d};
assertClose("compare vectors" ,result_mapAtan,v_mapAtan.toArray(),normTolerance);
//octave = atan(vat)
RealVector v_mapAtanToSelf = vat.copy();
v_mapAtanToSelf.mapToSelf(new Atan());
double[] result_mapAtanToSelf = {0.0d,4.636476090008061e-01d,7.853981633974483e-01d};
assertClose("compare vectors" ,result_mapAtanToSelf,v_mapAtanToSelf.toArray(),normTolerance);
//octave = v1 .^-1
RealVector v_mapInv = v1.map(new Inverse());
double[] result_mapInv = {1d,0.5d,3.333333333333333e-01d};
assertClose("compare vectors" ,result_mapInv,v_mapInv.toArray(),normTolerance);
//octave = v1 .^-1
RealVector v_mapInvToSelf = v1.copy();
v_mapInvToSelf.mapToSelf(new Inverse());
double[] result_mapInvToSelf = {1d,0.5d,3.333333333333333e-01d};
assertClose("compare vectors" ,result_mapInvToSelf,v_mapInvToSelf.toArray(),normTolerance);
double[] abs_a = {-1.0d, 0.0d, 1.0d};
ArrayRealVector abs_v = new ArrayRealVector(abs_a);
//octave = abs(abs_v)
RealVector v_mapAbs = abs_v.map(new Abs());
double[] result_mapAbs = {1d,0d,1d};
assertClose("compare vectors" ,result_mapAbs,v_mapAbs.toArray(),normTolerance);
//octave = abs(abs_v)
RealVector v_mapAbsToSelf = abs_v.copy();
v_mapAbsToSelf.mapToSelf(new Abs());
double[] result_mapAbsToSelf = {1d,0d,1d};
assertClose("compare vectors" ,result_mapAbsToSelf,v_mapAbsToSelf.toArray(),normTolerance);
//octave = sqrt(v1)
RealVector v_mapSqrt = v1.map(new Sqrt());
double[] result_mapSqrt = {1d,1.414213562373095e+00d,1.732050807568877e+00d};
assertClose("compare vectors" ,result_mapSqrt,v_mapSqrt.toArray(),normTolerance);
//octave = sqrt(v1)
RealVector v_mapSqrtToSelf = v1.copy();
v_mapSqrtToSelf.mapToSelf(new Sqrt());
double[] result_mapSqrtToSelf = {1d,1.414213562373095e+00d,1.732050807568877e+00d};
assertClose("compare vectors" ,result_mapSqrtToSelf,v_mapSqrtToSelf.toArray(),normTolerance);
double[] cbrt_a = {-2.0d, 0.0d, 2.0d};
ArrayRealVector cbrt_v = new ArrayRealVector(cbrt_a);
//octave = ???
RealVector v_mapCbrt = cbrt_v.map(new Cbrt());
double[] result_mapCbrt = {-1.2599210498948732d,0d,1.2599210498948732d};
assertClose("compare vectors" ,result_mapCbrt,v_mapCbrt.toArray(),normTolerance);
//octave = ???
RealVector v_mapCbrtToSelf = cbrt_v.copy();
v_mapCbrtToSelf.mapToSelf(new Cbrt());
double[] result_mapCbrtToSelf = {-1.2599210498948732d,0d,1.2599210498948732d};
assertClose("compare vectors" ,result_mapCbrtToSelf,v_mapCbrtToSelf.toArray(),normTolerance);
double[] ceil_a = {-1.1d, 0.9d, 1.1d};
ArrayRealVector ceil_v = new ArrayRealVector(ceil_a);
//octave = ceil(ceil_v)
RealVector v_mapCeil = ceil_v.map(new Ceil());
double[] result_mapCeil = {-1d,1d,2d};
assertClose("compare vectors" ,result_mapCeil,v_mapCeil.toArray(),normTolerance);
//octave = ceil(ceil_v)
RealVector v_mapCeilToSelf = ceil_v.copy();
v_mapCeilToSelf.mapToSelf(new Ceil());
double[] result_mapCeilToSelf = {-1d,1d,2d};
assertClose("compare vectors" ,result_mapCeilToSelf,v_mapCeilToSelf.toArray(),normTolerance);
//octave = floor(ceil_v)
RealVector v_mapFloor = ceil_v.map(new Floor());
double[] result_mapFloor = {-2d,0d,1d};
assertClose("compare vectors" ,result_mapFloor,v_mapFloor.toArray(),normTolerance);
//octave = floor(ceil_v)
RealVector v_mapFloorToSelf = ceil_v.copy();
v_mapFloorToSelf.mapToSelf(new Floor());
double[] result_mapFloorToSelf = {-2d,0d,1d};
assertClose("compare vectors" ,result_mapFloorToSelf,v_mapFloorToSelf.toArray(),normTolerance);
//octave = ???
RealVector v_mapRint = ceil_v.map(new Rint());
double[] result_mapRint = {-1d,1d,1d};
assertClose("compare vectors" ,result_mapRint,v_mapRint.toArray(),normTolerance);
//octave = ???
RealVector v_mapRintToSelf = ceil_v.copy();
v_mapRintToSelf.mapToSelf(new Rint());
double[] result_mapRintToSelf = {-1d,1d,1d};
assertClose("compare vectors" ,result_mapRintToSelf,v_mapRintToSelf.toArray(),normTolerance);
//octave = ???
RealVector v_mapSignum = ceil_v.map(new Signum());
double[] result_mapSignum = {-1d,1d,1d};
assertClose("compare vectors" ,result_mapSignum,v_mapSignum.toArray(),normTolerance);
//octave = ???
RealVector v_mapSignumToSelf = ceil_v.copy();
v_mapSignumToSelf.mapToSelf(new Signum());
double[] result_mapSignumToSelf = {-1d,1d,1d};
assertClose("compare vectors" ,result_mapSignumToSelf,v_mapSignumToSelf.toArray(),normTolerance);
// Is with the used resolutions of limited value as test
//octave = ???
RealVector v_mapUlp = ceil_v.map(new Ulp());
double[] result_mapUlp = {2.220446049250313E-16d,1.1102230246251565E-16d,2.220446049250313E-16d};
assertClose("compare vectors" ,result_mapUlp,v_mapUlp.toArray(),normTolerance);
//octave = ???
RealVector v_mapUlpToSelf = ceil_v.copy();
v_mapUlpToSelf.mapToSelf(new Ulp());
double[] result_mapUlpToSelf = {2.220446049250313E-16d,1.1102230246251565E-16d,2.220446049250313E-16d};
assertClose("compare vectors" ,result_mapUlpToSelf,v_mapUlpToSelf.toArray(),normTolerance);
}
@Test
public void testBasicFunctions() {
ArrayRealVector v1 = new ArrayRealVector(vec1);
@ -1334,17 +1003,6 @@ public class ArrayRealVectorTest extends RealVectorAbstractTest {
}
}
/** verifies that two vectors are close (sup norm) */
protected void assertClose(String msg, double[] m, double[] n,
double tolerance) {
if (m.length != n.length) {
Assert.fail("vectors have different lengths");
}
for (int i = 0; i < m.length; i++) {
Assert.assertEquals(msg + " " + i + " elements differ", m[i],n[i],tolerance);
}
}
@Override
public RealVector create(final double[] data) {
return new ArrayRealVector(data, true);

View File

@ -20,6 +20,30 @@ import java.util.Arrays;
import junit.framework.Assert;
import org.apache.commons.math3.analysis.function.Abs;
import org.apache.commons.math3.analysis.function.Acos;
import org.apache.commons.math3.analysis.function.Asin;
import org.apache.commons.math3.analysis.function.Atan;
import org.apache.commons.math3.analysis.function.Cbrt;
import org.apache.commons.math3.analysis.function.Ceil;
import org.apache.commons.math3.analysis.function.Cos;
import org.apache.commons.math3.analysis.function.Cosh;
import org.apache.commons.math3.analysis.function.Exp;
import org.apache.commons.math3.analysis.function.Expm1;
import org.apache.commons.math3.analysis.function.Floor;
import org.apache.commons.math3.analysis.function.Inverse;
import org.apache.commons.math3.analysis.function.Log;
import org.apache.commons.math3.analysis.function.Log10;
import org.apache.commons.math3.analysis.function.Log1p;
import org.apache.commons.math3.analysis.function.Power;
import org.apache.commons.math3.analysis.function.Rint;
import org.apache.commons.math3.analysis.function.Signum;
import org.apache.commons.math3.analysis.function.Sin;
import org.apache.commons.math3.analysis.function.Sinh;
import org.apache.commons.math3.analysis.function.Sqrt;
import org.apache.commons.math3.analysis.function.Tan;
import org.apache.commons.math3.analysis.function.Tanh;
import org.apache.commons.math3.analysis.function.Ulp;
import org.apache.commons.math3.exception.NumberIsTooSmallException;
import org.apache.commons.math3.exception.OutOfRangeException;
import org.junit.Test;
@ -45,6 +69,17 @@ public abstract class RealVectorAbstractTest {
*/
public abstract RealVector createAlien(double[] data);
/** verifies that two vectors are close (sup norm) */
protected void assertClose(String msg, double[] m, double[] n,
double tolerance) {
if (m.length != n.length) {
Assert.fail("vectors have different lengths");
}
for (int i = 0; i < m.length; i++) {
Assert.assertEquals(msg + " " + i + " elements differ", m[i],n[i],tolerance);
}
}
protected double[][] ma1 = {{1d, 2d, 3d}, {4d, 5d, 6d}, {7d, 8d, 9d}};
protected double[] vec1 = {1d, 2d, 3d};
protected double[] vec2 = {4d, 5d, 6d};
@ -55,6 +90,10 @@ public abstract class RealVectorAbstractTest {
protected Double[] dvec1 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d};
protected double[][] mat1 = {{1d, 2d, 3d}, {4d, 5d, 6d},{ 7d, 8d, 9d}};
// tolerances
protected double entryTolerance = 10E-16;
protected double normTolerance = 10E-14;
@Test
public void testDataInOut() {
final RealVector v1 = create(vec1);
@ -138,6 +177,335 @@ public abstract class RealVectorAbstractTest {
Assert.assertNotSame(vout10, vout10_2);
}
@Test
public void testMapFunctions() {
final RealVector v1 = create(vec1);
//octave = v1 .+ 2.0
RealVector v_mapAdd = v1.mapAdd(2.0d);
double[] result_mapAdd = {3d, 4d, 5d};
assertClose("compare vectors" ,result_mapAdd,v_mapAdd.toArray(),normTolerance);
//octave = v1 .+ 2.0
RealVector v_mapAddToSelf = v1.copy();
v_mapAddToSelf.mapAddToSelf(2.0d);
double[] result_mapAddToSelf = {3d, 4d, 5d};
assertClose("compare vectors" ,result_mapAddToSelf,v_mapAddToSelf.toArray(),normTolerance);
//octave = v1 .- 2.0
RealVector v_mapSubtract = v1.mapSubtract(2.0d);
double[] result_mapSubtract = {-1d, 0d, 1d};
assertClose("compare vectors" ,result_mapSubtract,v_mapSubtract.toArray(),normTolerance);
//octave = v1 .- 2.0
RealVector v_mapSubtractToSelf = v1.copy();
v_mapSubtractToSelf.mapSubtractToSelf(2.0d);
double[] result_mapSubtractToSelf = {-1d, 0d, 1d};
assertClose("compare vectors" ,result_mapSubtractToSelf,v_mapSubtractToSelf.toArray(),normTolerance);
//octave = v1 .* 2.0
RealVector v_mapMultiply = v1.mapMultiply(2.0d);
double[] result_mapMultiply = {2d, 4d, 6d};
assertClose("compare vectors" ,result_mapMultiply,v_mapMultiply.toArray(),normTolerance);
//octave = v1 .* 2.0
RealVector v_mapMultiplyToSelf = v1.copy();
v_mapMultiplyToSelf.mapMultiplyToSelf(2.0d);
double[] result_mapMultiplyToSelf = {2d, 4d, 6d};
assertClose("compare vectors" ,result_mapMultiplyToSelf,v_mapMultiplyToSelf.toArray(),normTolerance);
//octave = v1 ./ 2.0
RealVector v_mapDivide = v1.mapDivide(2.0d);
double[] result_mapDivide = {.5d, 1d, 1.5d};
assertClose("compare vectors" ,result_mapDivide,v_mapDivide.toArray(),normTolerance);
//octave = v1 ./ 2.0
RealVector v_mapDivideToSelf = v1.copy();
v_mapDivideToSelf.mapDivideToSelf(2.0d);
double[] result_mapDivideToSelf = {.5d, 1d, 1.5d};
assertClose("compare vectors" ,result_mapDivideToSelf,v_mapDivideToSelf.toArray(),normTolerance);
//octave = v1 .^ 2.0
RealVector v_mapPow = v1.map(new Power(2));
double[] result_mapPow = {1d, 4d, 9d};
assertClose("compare vectors" ,result_mapPow,v_mapPow.toArray(),normTolerance);
//octave = v1 .^ 2.0
RealVector v_mapPowToSelf = v1.copy();
v_mapPowToSelf.mapToSelf(new Power(2));
double[] result_mapPowToSelf = {1d, 4d, 9d};
assertClose("compare vectors" ,result_mapPowToSelf,v_mapPowToSelf.toArray(),normTolerance);
//octave = exp(v1)
RealVector v_mapExp = v1.map(new Exp());
double[] result_mapExp = {2.718281828459045e+00d,7.389056098930650e+00d, 2.008553692318767e+01d};
assertClose("compare vectors" ,result_mapExp,v_mapExp.toArray(),normTolerance);
//octave = exp(v1)
RealVector v_mapExpToSelf = v1.copy();
v_mapExpToSelf.mapToSelf(new Exp());
double[] result_mapExpToSelf = {2.718281828459045e+00d,7.389056098930650e+00d, 2.008553692318767e+01d};
assertClose("compare vectors" ,result_mapExpToSelf,v_mapExpToSelf.toArray(),normTolerance);
//octave = ???
RealVector v_mapExpm1 = v1.map(new Expm1());
double[] result_mapExpm1 = {1.718281828459045d,6.38905609893065d, 19.085536923187668d};
assertClose("compare vectors" ,result_mapExpm1,v_mapExpm1.toArray(),normTolerance);
//octave = ???
RealVector v_mapExpm1ToSelf = v1.copy();
v_mapExpm1ToSelf.mapToSelf(new Expm1());
double[] result_mapExpm1ToSelf = {1.718281828459045d,6.38905609893065d, 19.085536923187668d};
assertClose("compare vectors" ,result_mapExpm1ToSelf,v_mapExpm1ToSelf.toArray(),normTolerance);
//octave = log(v1)
RealVector v_mapLog = v1.map(new Log());
double[] result_mapLog = {0d,6.931471805599453e-01d, 1.098612288668110e+00d};
assertClose("compare vectors" ,result_mapLog,v_mapLog.toArray(),normTolerance);
//octave = log(v1)
RealVector v_mapLogToSelf = v1.copy();
v_mapLogToSelf.mapToSelf(new Log());
double[] result_mapLogToSelf = {0d,6.931471805599453e-01d, 1.098612288668110e+00d};
assertClose("compare vectors" ,result_mapLogToSelf,v_mapLogToSelf.toArray(),normTolerance);
//octave = log10(v1)
RealVector v_mapLog10 = v1.map(new Log10());
double[] result_mapLog10 = {0d,3.010299956639812e-01d, 4.771212547196624e-01d};
assertClose("compare vectors" ,result_mapLog10,v_mapLog10.toArray(),normTolerance);
//octave = log(v1)
RealVector v_mapLog10ToSelf = v1.copy();
v_mapLog10ToSelf.mapToSelf(new Log10());
double[] result_mapLog10ToSelf = {0d,3.010299956639812e-01d, 4.771212547196624e-01d};
assertClose("compare vectors" ,result_mapLog10ToSelf,v_mapLog10ToSelf.toArray(),normTolerance);
//octave = ???
RealVector v_mapLog1p = v1.map(new Log1p());
double[] result_mapLog1p = {0.6931471805599453d,1.0986122886681096d,1.3862943611198906d};
assertClose("compare vectors" ,result_mapLog1p,v_mapLog1p.toArray(),normTolerance);
//octave = ???
RealVector v_mapLog1pToSelf = v1.copy();
v_mapLog1pToSelf.mapToSelf(new Log1p());
double[] result_mapLog1pToSelf = {0.6931471805599453d,1.0986122886681096d,1.3862943611198906d};
assertClose("compare vectors" ,result_mapLog1pToSelf,v_mapLog1pToSelf.toArray(),normTolerance);
//octave = cosh(v1)
RealVector v_mapCosh = v1.map(new Cosh());
double[] result_mapCosh = {1.543080634815244e+00d,3.762195691083631e+00d, 1.006766199577777e+01d};
assertClose("compare vectors" ,result_mapCosh,v_mapCosh.toArray(),normTolerance);
//octave = cosh(v1)
RealVector v_mapCoshToSelf = v1.copy();
v_mapCoshToSelf.mapToSelf(new Cosh());
double[] result_mapCoshToSelf = {1.543080634815244e+00d,3.762195691083631e+00d, 1.006766199577777e+01d};
assertClose("compare vectors" ,result_mapCoshToSelf,v_mapCoshToSelf.toArray(),normTolerance);
//octave = sinh(v1)
RealVector v_mapSinh = v1.map(new Sinh());
double[] result_mapSinh = {1.175201193643801e+00d,3.626860407847019e+00d, 1.001787492740990e+01d};
assertClose("compare vectors" ,result_mapSinh,v_mapSinh.toArray(),normTolerance);
//octave = sinh(v1)
RealVector v_mapSinhToSelf = v1.copy();
v_mapSinhToSelf.mapToSelf(new Sinh());
double[] result_mapSinhToSelf = {1.175201193643801e+00d,3.626860407847019e+00d, 1.001787492740990e+01d};
assertClose("compare vectors" ,result_mapSinhToSelf,v_mapSinhToSelf.toArray(),normTolerance);
//octave = tanh(v1)
RealVector v_mapTanh = v1.map(new Tanh());
double[] result_mapTanh = {7.615941559557649e-01d,9.640275800758169e-01d,9.950547536867305e-01d};
assertClose("compare vectors" ,result_mapTanh,v_mapTanh.toArray(),normTolerance);
//octave = tanh(v1)
RealVector v_mapTanhToSelf = v1.copy();
v_mapTanhToSelf.mapToSelf(new Tanh());
double[] result_mapTanhToSelf = {7.615941559557649e-01d,9.640275800758169e-01d,9.950547536867305e-01d};
assertClose("compare vectors" ,result_mapTanhToSelf,v_mapTanhToSelf.toArray(),normTolerance);
//octave = cos(v1)
RealVector v_mapCos = v1.map(new Cos());
double[] result_mapCos = {5.403023058681398e-01d,-4.161468365471424e-01d, -9.899924966004454e-01d};
assertClose("compare vectors" ,result_mapCos,v_mapCos.toArray(),normTolerance);
//octave = cos(v1)
RealVector v_mapCosToSelf = v1.copy();
v_mapCosToSelf.mapToSelf(new Cos());
double[] result_mapCosToSelf = {5.403023058681398e-01d,-4.161468365471424e-01d, -9.899924966004454e-01d};
assertClose("compare vectors" ,result_mapCosToSelf,v_mapCosToSelf.toArray(),normTolerance);
//octave = sin(v1)
RealVector v_mapSin = v1.map(new Sin());
double[] result_mapSin = {8.414709848078965e-01d,9.092974268256817e-01d,1.411200080598672e-01d};
assertClose("compare vectors" ,result_mapSin,v_mapSin.toArray(),normTolerance);
//octave = sin(v1)
RealVector v_mapSinToSelf = v1.copy();
v_mapSinToSelf.mapToSelf(new Sin());
double[] result_mapSinToSelf = {8.414709848078965e-01d,9.092974268256817e-01d,1.411200080598672e-01d};
assertClose("compare vectors" ,result_mapSinToSelf,v_mapSinToSelf.toArray(),normTolerance);
//octave = tan(v1)
RealVector v_mapTan = v1.map(new Tan());
double[] result_mapTan = {1.557407724654902e+00d,-2.185039863261519e+00d,-1.425465430742778e-01d};
assertClose("compare vectors" ,result_mapTan,v_mapTan.toArray(),normTolerance);
//octave = tan(v1)
RealVector v_mapTanToSelf = v1.copy();
v_mapTanToSelf.mapToSelf(new Tan());
double[] result_mapTanToSelf = {1.557407724654902e+00d,-2.185039863261519e+00d,-1.425465430742778e-01d};
assertClose("compare vectors" ,result_mapTanToSelf,v_mapTanToSelf.toArray(),normTolerance);
double[] vat_a = {0d, 0.5d, 1.0d};
final RealVector vat = create(vat_a);
//octave = acos(vat)
RealVector v_mapAcos = vat.map(new Acos());
double[] result_mapAcos = {1.570796326794897e+00d,1.047197551196598e+00d, 0.0d};
assertClose("compare vectors" ,result_mapAcos,v_mapAcos.toArray(),normTolerance);
//octave = acos(vat)
RealVector v_mapAcosToSelf = vat.copy();
v_mapAcosToSelf.mapToSelf(new Acos());
double[] result_mapAcosToSelf = {1.570796326794897e+00d,1.047197551196598e+00d, 0.0d};
assertClose("compare vectors" ,result_mapAcosToSelf,v_mapAcosToSelf.toArray(),normTolerance);
//octave = asin(vat)
RealVector v_mapAsin = vat.map(new Asin());
double[] result_mapAsin = {0.0d,5.235987755982989e-01d,1.570796326794897e+00d};
assertClose("compare vectors" ,result_mapAsin,v_mapAsin.toArray(),normTolerance);
//octave = asin(vat)
RealVector v_mapAsinToSelf = vat.copy();
v_mapAsinToSelf.mapToSelf(new Asin());
double[] result_mapAsinToSelf = {0.0d,5.235987755982989e-01d,1.570796326794897e+00d};
assertClose("compare vectors" ,result_mapAsinToSelf,v_mapAsinToSelf.toArray(),normTolerance);
//octave = atan(vat)
RealVector v_mapAtan = vat.map(new Atan());
double[] result_mapAtan = {0.0d,4.636476090008061e-01d,7.853981633974483e-01d};
assertClose("compare vectors" ,result_mapAtan,v_mapAtan.toArray(),normTolerance);
//octave = atan(vat)
RealVector v_mapAtanToSelf = vat.copy();
v_mapAtanToSelf.mapToSelf(new Atan());
double[] result_mapAtanToSelf = {0.0d,4.636476090008061e-01d,7.853981633974483e-01d};
assertClose("compare vectors" ,result_mapAtanToSelf,v_mapAtanToSelf.toArray(),normTolerance);
//octave = v1 .^-1
RealVector v_mapInv = v1.map(new Inverse());
double[] result_mapInv = {1d,0.5d,3.333333333333333e-01d};
assertClose("compare vectors" ,result_mapInv,v_mapInv.toArray(),normTolerance);
//octave = v1 .^-1
RealVector v_mapInvToSelf = v1.copy();
v_mapInvToSelf.mapToSelf(new Inverse());
double[] result_mapInvToSelf = {1d,0.5d,3.333333333333333e-01d};
assertClose("compare vectors" ,result_mapInvToSelf,v_mapInvToSelf.toArray(),normTolerance);
double[] abs_a = {-1.0d, 0.0d, 1.0d};
final RealVector abs_v = create(abs_a);
//octave = abs(abs_v)
RealVector v_mapAbs = abs_v.map(new Abs());
double[] result_mapAbs = {1d,0d,1d};
assertClose("compare vectors" ,result_mapAbs,v_mapAbs.toArray(),normTolerance);
//octave = abs(abs_v)
RealVector v_mapAbsToSelf = abs_v.copy();
v_mapAbsToSelf.mapToSelf(new Abs());
double[] result_mapAbsToSelf = {1d,0d,1d};
assertClose("compare vectors" ,result_mapAbsToSelf,v_mapAbsToSelf.toArray(),normTolerance);
//octave = sqrt(v1)
RealVector v_mapSqrt = v1.map(new Sqrt());
double[] result_mapSqrt = {1d,1.414213562373095e+00d,1.732050807568877e+00d};
assertClose("compare vectors" ,result_mapSqrt,v_mapSqrt.toArray(),normTolerance);
//octave = sqrt(v1)
RealVector v_mapSqrtToSelf = v1.copy();
v_mapSqrtToSelf.mapToSelf(new Sqrt());
double[] result_mapSqrtToSelf = {1d,1.414213562373095e+00d,1.732050807568877e+00d};
assertClose("compare vectors" ,result_mapSqrtToSelf,v_mapSqrtToSelf.toArray(),normTolerance);
double[] cbrt_a = {-2.0d, 0.0d, 2.0d};
final RealVector cbrt_v = create(cbrt_a);
//octave = ???
RealVector v_mapCbrt = cbrt_v.map(new Cbrt());
double[] result_mapCbrt = {-1.2599210498948732d,0d,1.2599210498948732d};
assertClose("compare vectors" ,result_mapCbrt,v_mapCbrt.toArray(),normTolerance);
//octave = ???
RealVector v_mapCbrtToSelf = cbrt_v.copy();
v_mapCbrtToSelf.mapToSelf(new Cbrt());
double[] result_mapCbrtToSelf = {-1.2599210498948732d,0d,1.2599210498948732d};
assertClose("compare vectors" ,result_mapCbrtToSelf,v_mapCbrtToSelf.toArray(),normTolerance);
double[] ceil_a = {-1.1d, 0.9d, 1.1d};
ArrayRealVector ceil_v = new ArrayRealVector(ceil_a);
//octave = ceil(ceil_v)
RealVector v_mapCeil = ceil_v.map(new Ceil());
double[] result_mapCeil = {-1d,1d,2d};
assertClose("compare vectors" ,result_mapCeil,v_mapCeil.toArray(),normTolerance);
//octave = ceil(ceil_v)
RealVector v_mapCeilToSelf = ceil_v.copy();
v_mapCeilToSelf.mapToSelf(new Ceil());
double[] result_mapCeilToSelf = {-1d,1d,2d};
assertClose("compare vectors" ,result_mapCeilToSelf,v_mapCeilToSelf.toArray(),normTolerance);
//octave = floor(ceil_v)
RealVector v_mapFloor = ceil_v.map(new Floor());
double[] result_mapFloor = {-2d,0d,1d};
assertClose("compare vectors" ,result_mapFloor,v_mapFloor.toArray(),normTolerance);
//octave = floor(ceil_v)
RealVector v_mapFloorToSelf = ceil_v.copy();
v_mapFloorToSelf.mapToSelf(new Floor());
double[] result_mapFloorToSelf = {-2d,0d,1d};
assertClose("compare vectors" ,result_mapFloorToSelf,v_mapFloorToSelf.toArray(),normTolerance);
//octave = ???
RealVector v_mapRint = ceil_v.map(new Rint());
double[] result_mapRint = {-1d,1d,1d};
assertClose("compare vectors" ,result_mapRint,v_mapRint.toArray(),normTolerance);
//octave = ???
RealVector v_mapRintToSelf = ceil_v.copy();
v_mapRintToSelf.mapToSelf(new Rint());
double[] result_mapRintToSelf = {-1d,1d,1d};
assertClose("compare vectors" ,result_mapRintToSelf,v_mapRintToSelf.toArray(),normTolerance);
//octave = ???
RealVector v_mapSignum = ceil_v.map(new Signum());
double[] result_mapSignum = {-1d,1d,1d};
assertClose("compare vectors" ,result_mapSignum,v_mapSignum.toArray(),normTolerance);
//octave = ???
RealVector v_mapSignumToSelf = ceil_v.copy();
v_mapSignumToSelf.mapToSelf(new Signum());
double[] result_mapSignumToSelf = {-1d,1d,1d};
assertClose("compare vectors" ,result_mapSignumToSelf,v_mapSignumToSelf.toArray(),normTolerance);
// Is with the used resolutions of limited value as test
//octave = ???
RealVector v_mapUlp = ceil_v.map(new Ulp());
double[] result_mapUlp = {2.220446049250313E-16d,1.1102230246251565E-16d,2.220446049250313E-16d};
assertClose("compare vectors" ,result_mapUlp,v_mapUlp.toArray(),normTolerance);
//octave = ???
RealVector v_mapUlpToSelf = ceil_v.copy();
v_mapUlpToSelf.mapToSelf(new Ulp());
double[] result_mapUlpToSelf = {2.220446049250313E-16d,1.1102230246251565E-16d,2.220446049250313E-16d};
assertClose("compare vectors" ,result_mapUlpToSelf,v_mapUlpToSelf.toArray(),normTolerance);
}
/*
* TESTS OF THE VISITOR PATTERN
*/

View File

@ -58,10 +58,6 @@ import org.apache.commons.math3.analysis.function.Power;
*/
public class SparseRealVectorTest extends RealVectorAbstractTest {
// tolerances
protected double entryTolerance = 10E-16;
protected double normTolerance = 10E-14;
// Testclass to test the RealVector interface
// only with enough content to support the test
public static class SparseRealVectorTestImpl extends RealVector implements Serializable {
@ -327,334 +323,6 @@ public class SparseRealVectorTest extends RealVectorAbstractTest {
}
@Test
public void testMapFunctions() {
OpenMapRealVector v1 = new OpenMapRealVector(vec1);
//octave = v1 .+ 2.0
RealVector v_mapAdd = v1.mapAdd(2.0d);
double[] result_mapAdd = {3d, 4d, 5d};
assertClose("compare vectors" ,result_mapAdd,v_mapAdd.toArray(),normTolerance);
//octave = v1 .+ 2.0
RealVector v_mapAddToSelf = v1.copy();
v_mapAddToSelf.mapAddToSelf(2.0d);
double[] result_mapAddToSelf = {3d, 4d, 5d};
assertClose("compare vectors" ,result_mapAddToSelf,v_mapAddToSelf.toArray(),normTolerance);
//octave = v1 .- 2.0
RealVector v_mapSubtract = v1.mapSubtract(2.0d);
double[] result_mapSubtract = {-1d, 0d, 1d};
assertClose("compare vectors" ,result_mapSubtract,v_mapSubtract.toArray(),normTolerance);
//octave = v1 .- 2.0
RealVector v_mapSubtractToSelf = v1.copy();
v_mapSubtractToSelf.mapSubtractToSelf(2.0d);
double[] result_mapSubtractToSelf = {-1d, 0d, 1d};
assertClose("compare vectors" ,result_mapSubtractToSelf,v_mapSubtractToSelf.toArray(),normTolerance);
//octave = v1 .* 2.0
RealVector v_mapMultiply = v1.mapMultiply(2.0d);
double[] result_mapMultiply = {2d, 4d, 6d};
assertClose("compare vectors" ,result_mapMultiply,v_mapMultiply.toArray(),normTolerance);
//octave = v1 .* 2.0
RealVector v_mapMultiplyToSelf = v1.copy();
v_mapMultiplyToSelf.mapMultiplyToSelf(2.0d);
double[] result_mapMultiplyToSelf = {2d, 4d, 6d};
assertClose("compare vectors" ,result_mapMultiplyToSelf,v_mapMultiplyToSelf.toArray(),normTolerance);
//octave = v1 ./ 2.0
RealVector v_mapDivide = v1.mapDivide(2.0d);
double[] result_mapDivide = {.5d, 1d, 1.5d};
assertClose("compare vectors" ,result_mapDivide,v_mapDivide.toArray(),normTolerance);
//octave = v1 ./ 2.0
RealVector v_mapDivideToSelf = v1.copy();
v_mapDivideToSelf.mapDivideToSelf(2.0d);
double[] result_mapDivideToSelf = {.5d, 1d, 1.5d};
assertClose("compare vectors" ,result_mapDivideToSelf,v_mapDivideToSelf.toArray(),normTolerance);
//octave = v1 .^ 2.0
RealVector v_mapPow = v1.map(new Power(2));
double[] result_mapPow = {1d, 4d, 9d};
assertClose("compare vectors" ,result_mapPow,v_mapPow.toArray(),normTolerance);
//octave = v1 .^ 2.0
RealVector v_mapPowToSelf = v1.copy();
v_mapPowToSelf.mapToSelf(new Power(2));
double[] result_mapPowToSelf = {1d, 4d, 9d};
assertClose("compare vectors" ,result_mapPowToSelf,v_mapPowToSelf.toArray(),normTolerance);
//octave = exp(v1)
RealVector v_mapExp = v1.map(new Exp());
double[] result_mapExp = {2.718281828459045e+00d,7.389056098930650e+00d, 2.008553692318767e+01d};
assertClose("compare vectors" ,result_mapExp,v_mapExp.toArray(),normTolerance);
//octave = exp(v1)
RealVector v_mapExpToSelf = v1.copy();
v_mapExpToSelf.mapToSelf(new Exp());
double[] result_mapExpToSelf = {2.718281828459045e+00d,7.389056098930650e+00d, 2.008553692318767e+01d};
assertClose("compare vectors" ,result_mapExpToSelf,v_mapExpToSelf.toArray(),normTolerance);
//octave = ???
RealVector v_mapExpm1 = v1.map(new Expm1());
double[] result_mapExpm1 = {1.718281828459045d,6.38905609893065d, 19.085536923187668d};
assertClose("compare vectors" ,result_mapExpm1,v_mapExpm1.toArray(),normTolerance);
//octave = ???
RealVector v_mapExpm1ToSelf = v1.copy();
v_mapExpm1ToSelf.mapToSelf(new Expm1());
double[] result_mapExpm1ToSelf = {1.718281828459045d,6.38905609893065d, 19.085536923187668d};
assertClose("compare vectors" ,result_mapExpm1ToSelf,v_mapExpm1ToSelf.toArray(),normTolerance);
//octave = log(v1)
RealVector v_mapLog = v1.map(new Log());
double[] result_mapLog = {0d,6.931471805599453e-01d, 1.098612288668110e+00d};
assertClose("compare vectors" ,result_mapLog,v_mapLog.toArray(),normTolerance);
//octave = log(v1)
RealVector v_mapLogToSelf = v1.copy();
v_mapLogToSelf.mapToSelf(new Log());
double[] result_mapLogToSelf = {0d,6.931471805599453e-01d, 1.098612288668110e+00d};
assertClose("compare vectors" ,result_mapLogToSelf,v_mapLogToSelf.toArray(),normTolerance);
//octave = log10(v1)
RealVector v_mapLog10 = v1.map(new Log10());
double[] result_mapLog10 = {0d,3.010299956639812e-01d, 4.771212547196624e-01d};
assertClose("compare vectors" ,result_mapLog10,v_mapLog10.toArray(),normTolerance);
//octave = log(v1)
RealVector v_mapLog10ToSelf = v1.copy();
v_mapLog10ToSelf.mapToSelf(new Log10());
double[] result_mapLog10ToSelf = {0d,3.010299956639812e-01d, 4.771212547196624e-01d};
assertClose("compare vectors" ,result_mapLog10ToSelf,v_mapLog10ToSelf.toArray(),normTolerance);
//octave = ???
RealVector v_mapLog1p = v1.map(new Log1p());
double[] result_mapLog1p = {0.6931471805599453d,1.0986122886681096d,1.3862943611198906d};
assertClose("compare vectors" ,result_mapLog1p,v_mapLog1p.toArray(),normTolerance);
//octave = ???
RealVector v_mapLog1pToSelf = v1.copy();
v_mapLog1pToSelf.mapToSelf(new Log1p());
double[] result_mapLog1pToSelf = {0.6931471805599453d,1.0986122886681096d,1.3862943611198906d};
assertClose("compare vectors" ,result_mapLog1pToSelf,v_mapLog1pToSelf.toArray(),normTolerance);
//octave = cosh(v1)
RealVector v_mapCosh = v1.map(new Cosh());
double[] result_mapCosh = {1.543080634815244e+00d,3.762195691083631e+00d, 1.006766199577777e+01d};
assertClose("compare vectors" ,result_mapCosh,v_mapCosh.toArray(),normTolerance);
//octave = cosh(v1)
RealVector v_mapCoshToSelf = v1.copy();
v_mapCoshToSelf.mapToSelf(new Cosh());
double[] result_mapCoshToSelf = {1.543080634815244e+00d,3.762195691083631e+00d, 1.006766199577777e+01d};
assertClose("compare vectors" ,result_mapCoshToSelf,v_mapCoshToSelf.toArray(),normTolerance);
//octave = sinh(v1)
RealVector v_mapSinh = v1.map(new Sinh());
double[] result_mapSinh = {1.175201193643801e+00d,3.626860407847019e+00d, 1.001787492740990e+01d};
assertClose("compare vectors" ,result_mapSinh,v_mapSinh.toArray(),normTolerance);
//octave = sinh(v1)
RealVector v_mapSinhToSelf = v1.copy();
v_mapSinhToSelf.mapToSelf(new Sinh());
double[] result_mapSinhToSelf = {1.175201193643801e+00d,3.626860407847019e+00d, 1.001787492740990e+01d};
assertClose("compare vectors" ,result_mapSinhToSelf,v_mapSinhToSelf.toArray(),normTolerance);
//octave = tanh(v1)
RealVector v_mapTanh = v1.map(new Tanh());
double[] result_mapTanh = {7.615941559557649e-01d,9.640275800758169e-01d,9.950547536867305e-01d};
assertClose("compare vectors" ,result_mapTanh,v_mapTanh.toArray(),normTolerance);
//octave = tanh(v1)
RealVector v_mapTanhToSelf = v1.copy();
v_mapTanhToSelf.mapToSelf(new Tanh());
double[] result_mapTanhToSelf = {7.615941559557649e-01d,9.640275800758169e-01d,9.950547536867305e-01d};
assertClose("compare vectors" ,result_mapTanhToSelf,v_mapTanhToSelf.toArray(),normTolerance);
//octave = cos(v1)
RealVector v_mapCos = v1.map(new Cos());
double[] result_mapCos = {5.403023058681398e-01d,-4.161468365471424e-01d, -9.899924966004454e-01d};
assertClose("compare vectors" ,result_mapCos,v_mapCos.toArray(),normTolerance);
//octave = cos(v1)
RealVector v_mapCosToSelf = v1.copy();
v_mapCosToSelf.mapToSelf(new Cos());
double[] result_mapCosToSelf = {5.403023058681398e-01d,-4.161468365471424e-01d, -9.899924966004454e-01d};
assertClose("compare vectors" ,result_mapCosToSelf,v_mapCosToSelf.toArray(),normTolerance);
//octave = sin(v1)
RealVector v_mapSin = v1.map(new Sin());
double[] result_mapSin = {8.414709848078965e-01d,9.092974268256817e-01d,1.411200080598672e-01d};
assertClose("compare vectors" ,result_mapSin,v_mapSin.toArray(),normTolerance);
//octave = sin(v1)
RealVector v_mapSinToSelf = v1.copy();
v_mapSinToSelf.mapToSelf(new Sin());
double[] result_mapSinToSelf = {8.414709848078965e-01d,9.092974268256817e-01d,1.411200080598672e-01d};
assertClose("compare vectors" ,result_mapSinToSelf,v_mapSinToSelf.toArray(),normTolerance);
//octave = tan(v1)
RealVector v_mapTan = v1.map(new Tan());
double[] result_mapTan = {1.557407724654902e+00d,-2.185039863261519e+00d,-1.425465430742778e-01d};
assertClose("compare vectors" ,result_mapTan,v_mapTan.toArray(),normTolerance);
//octave = tan(v1)
RealVector v_mapTanToSelf = v1.copy();
v_mapTanToSelf.mapToSelf(new Tan());
double[] result_mapTanToSelf = {1.557407724654902e+00d,-2.185039863261519e+00d,-1.425465430742778e-01d};
assertClose("compare vectors" ,result_mapTanToSelf,v_mapTanToSelf.toArray(),normTolerance);
double[] vat_a = {0d, 0.5d, 1.0d};
OpenMapRealVector vat = new OpenMapRealVector(vat_a);
//octave = acos(vat)
RealVector v_mapAcos = vat.map(new Acos());
double[] result_mapAcos = {1.570796326794897e+00d,1.047197551196598e+00d, 0.0d};
assertClose("compare vectors" ,result_mapAcos,v_mapAcos.toArray(),normTolerance);
//octave = acos(vat)
RealVector v_mapAcosToSelf = vat.copy();
v_mapAcosToSelf.mapToSelf(new Acos());
double[] result_mapAcosToSelf = {1.570796326794897e+00d,1.047197551196598e+00d, 0.0d};
assertClose("compare vectors" ,result_mapAcosToSelf,v_mapAcosToSelf.toArray(),normTolerance);
//octave = asin(vat)
RealVector v_mapAsin = vat.map(new Asin());
double[] result_mapAsin = {0.0d,5.235987755982989e-01d,1.570796326794897e+00d};
assertClose("compare vectors" ,result_mapAsin,v_mapAsin.toArray(),normTolerance);
//octave = asin(vat)
RealVector v_mapAsinToSelf = vat.copy();
v_mapAsinToSelf.mapToSelf(new Asin());
double[] result_mapAsinToSelf = {0.0d,5.235987755982989e-01d,1.570796326794897e+00d};
assertClose("compare vectors" ,result_mapAsinToSelf,v_mapAsinToSelf.toArray(),normTolerance);
//octave = atan(vat)
RealVector v_mapAtan = vat.map(new Atan());
double[] result_mapAtan = {0.0d,4.636476090008061e-01d,7.853981633974483e-01d};
assertClose("compare vectors" ,result_mapAtan,v_mapAtan.toArray(),normTolerance);
//octave = atan(vat)
RealVector v_mapAtanToSelf = vat.copy();
v_mapAtanToSelf.mapToSelf(new Atan());
double[] result_mapAtanToSelf = {0.0d,4.636476090008061e-01d,7.853981633974483e-01d};
assertClose("compare vectors" ,result_mapAtanToSelf,v_mapAtanToSelf.toArray(),normTolerance);
//octave = v1 .^-1
RealVector v_mapInv = v1.map(new Inverse());
double[] result_mapInv = {1d,0.5d,3.333333333333333e-01d};
assertClose("compare vectors" ,result_mapInv,v_mapInv.toArray(),normTolerance);
//octave = v1 .^-1
RealVector v_mapInvToSelf = v1.copy();
v_mapInvToSelf.mapToSelf(new Inverse());
double[] result_mapInvToSelf = {1d,0.5d,3.333333333333333e-01d};
assertClose("compare vectors" ,result_mapInvToSelf,v_mapInvToSelf.toArray(),normTolerance);
double[] abs_a = {-1.0d, 0.0d, 1.0d};
OpenMapRealVector abs_v = new OpenMapRealVector(abs_a);
//octave = abs(abs_v)
RealVector v_mapAbs = abs_v.map(new Abs());
double[] result_mapAbs = {1d,0d,1d};
assertClose("compare vectors" ,result_mapAbs,v_mapAbs.toArray(),normTolerance);
//octave = abs(abs_v)
RealVector v_mapAbsToSelf = abs_v.copy();
v_mapAbsToSelf.mapToSelf(new Abs());
double[] result_mapAbsToSelf = {1d,0d,1d};
assertClose("compare vectors" ,result_mapAbsToSelf,v_mapAbsToSelf.toArray(),normTolerance);
//octave = sqrt(v1)
RealVector v_mapSqrt = v1.map(new Sqrt());
double[] result_mapSqrt = {1d,1.414213562373095e+00d,1.732050807568877e+00d};
assertClose("compare vectors" ,result_mapSqrt,v_mapSqrt.toArray(),normTolerance);
//octave = sqrt(v1)
RealVector v_mapSqrtToSelf = v1.copy();
v_mapSqrtToSelf.mapToSelf(new Sqrt());
double[] result_mapSqrtToSelf = {1d,1.414213562373095e+00d,1.732050807568877e+00d};
assertClose("compare vectors" ,result_mapSqrtToSelf,v_mapSqrtToSelf.toArray(),normTolerance);
double[] cbrt_a = {-2.0d, 0.0d, 2.0d};
OpenMapRealVector cbrt_v = new OpenMapRealVector(cbrt_a);
//octave = ???
RealVector v_mapCbrt = cbrt_v.map(new Cbrt());
double[] result_mapCbrt = {-1.2599210498948732d,0d,1.2599210498948732d};
assertClose("compare vectors" ,result_mapCbrt,v_mapCbrt.toArray(),normTolerance);
//octave = ???
RealVector v_mapCbrtToSelf = cbrt_v.copy();
v_mapCbrtToSelf.mapToSelf(new Cbrt());
double[] result_mapCbrtToSelf = {-1.2599210498948732d,0d,1.2599210498948732d};
assertClose("compare vectors" ,result_mapCbrtToSelf,v_mapCbrtToSelf.toArray(),normTolerance);
double[] ceil_a = {-1.1d, 0.9d, 1.1d};
OpenMapRealVector ceil_v = new OpenMapRealVector(ceil_a);
//octave = ceil(ceil_v)
RealVector v_mapCeil = ceil_v.map(new Ceil());
double[] result_mapCeil = {-1d,1d,2d};
assertClose("compare vectors" ,result_mapCeil,v_mapCeil.toArray(),normTolerance);
//octave = ceil(ceil_v)
RealVector v_mapCeilToSelf = ceil_v.copy();
v_mapCeilToSelf.mapToSelf(new Ceil());
double[] result_mapCeilToSelf = {-1d,1d,2d};
assertClose("compare vectors" ,result_mapCeilToSelf,v_mapCeilToSelf.toArray(),normTolerance);
//octave = floor(ceil_v)
RealVector v_mapFloor = ceil_v.map(new Floor());
double[] result_mapFloor = {-2d,0d,1d};
assertClose("compare vectors" ,result_mapFloor,v_mapFloor.toArray(),normTolerance);
//octave = floor(ceil_v)
RealVector v_mapFloorToSelf = ceil_v.copy();
v_mapFloorToSelf.mapToSelf(new Floor());
double[] result_mapFloorToSelf = {-2d,0d,1d};
assertClose("compare vectors" ,result_mapFloorToSelf,v_mapFloorToSelf.toArray(),normTolerance);
//octave = ???
RealVector v_mapRint = ceil_v.map(new Rint());
double[] result_mapRint = {-1d,1d,1d};
assertClose("compare vectors" ,result_mapRint,v_mapRint.toArray(),normTolerance);
//octave = ???
RealVector v_mapRintToSelf = ceil_v.copy();
v_mapRintToSelf.mapToSelf(new Rint());
double[] result_mapRintToSelf = {-1d,1d,1d};
assertClose("compare vectors" ,result_mapRintToSelf,v_mapRintToSelf.toArray(),normTolerance);
//octave = ???
RealVector v_mapSignum = ceil_v.map(new Signum());
double[] result_mapSignum = {-1d,1d,1d};
assertClose("compare vectors" ,result_mapSignum,v_mapSignum.toArray(),normTolerance);
//octave = ???
RealVector v_mapSignumToSelf = ceil_v.copy();
v_mapSignumToSelf.mapToSelf(new Signum());
double[] result_mapSignumToSelf = {-1d,1d,1d};
assertClose("compare vectors" ,result_mapSignumToSelf,v_mapSignumToSelf.toArray(),normTolerance);
// Is with the used resolutions of limited value as test
//octave = ???
RealVector v_mapUlp = ceil_v.map(new Ulp());
double[] result_mapUlp = {2.220446049250313E-16d,1.1102230246251565E-16d,2.220446049250313E-16d};
assertClose("compare vectors" ,result_mapUlp,v_mapUlp.toArray(),normTolerance);
//octave = ???
RealVector v_mapUlpToSelf = ceil_v.copy();
v_mapUlpToSelf.mapToSelf(new Ulp());
double[] result_mapUlpToSelf = {2.220446049250313E-16d,1.1102230246251565E-16d,2.220446049250313E-16d};
assertClose("compare vectors" ,result_mapUlpToSelf,v_mapUlpToSelf.toArray(),normTolerance);
}
@Test
public void testBasicFunctions() {
OpenMapRealVector v1 = new OpenMapRealVector(vec1);
@ -840,17 +508,6 @@ public class SparseRealVectorTest extends RealVectorAbstractTest {
Assert.assertEquals(v,TestUtils.serializeAndRecover(v));
}
/** verifies that two vectors are close (sup norm) */
protected void assertClose(String msg, double[] m, double[] n,
double tolerance) {
if (m.length != n.length) {
Assert.fail("vectors have different lengths");
}
for (int i = 0; i < m.length; i++) {
Assert.assertEquals(msg + " " + i + " elements differ", m[i],n[i],tolerance);
}
}
/* Check that the operations do not throw an exception (cf. MATH-645). */
@Test
public void testConcurrentModification() {