@Override public IComplexNumber exp() { IComplexNumber result = dup(); double realExp = FastMath.exp(realComponent()); return result.set( realExp * FastMath.cos(imaginaryComponent()), realExp * FastMath.sin(imaginaryComponent())); }
@Override public IComplexNumber powi(IComplexNumber c, IComplexNumber result) { IComplexNumber eval = log().muli(c).exp(); result.set(eval.realComponent(), eval.imaginaryComponent()); result.set(eval.realComponent(), eval.imaginaryComponent()); return result; }
@Test public void testComplexCalculation() { IComplexNDArray arr = Nd4j.createComplex( new IComplexNumber[][] { {Nd4j.createComplexNumber(1, 1), Nd4j.createComplexNumber(2, 1)}, {Nd4j.createComplexNumber(3, 2), Nd4j.createComplexNumber(4, 2)} }); IComplexNumber scalar = arr.sumComplex(); double sum = scalar.realComponent().doubleValue(); assertEquals(10, sum, 1e-1); double sumImag = scalar.imaginaryComponent().doubleValue(); assertEquals(6, sumImag, 1e-1); IComplexNDArray res = arr.add(Nd4j.createComplexNumber(1, 1)); scalar = res.sumComplex(); sum = scalar.realComponent().doubleValue(); assertEquals(14, sum, 1e-1); sumImag = scalar.imaginaryComponent().doubleValue(); assertEquals(10, sumImag, 1e-1); // original array should keep as it is sum = arr.sumComplex().realComponent().doubleValue(); assertEquals(10, sum, 1e-1); }
/** * Multiply two complex numbers, inplace * * @param c * @param result */ @Override public IComplexNumber muli(IComplexNumber c, IComplexNumber result) { float newR = real() * c.realComponent().floatValue() - imag() * c.imaginaryComponent().floatValue(); float newI = real() * c.imaginaryComponent().floatValue() + imag() * c.realComponent().floatValue(); result.set(newR, newI); return result; }
@Override public IComplexNumber log() { IComplexNumber result = dup(); float real = (float) result.realComponent(); float imaginary = (float) result.imaginaryComponent(); double modulus = FastMath.sqrt(real * real + imaginary * imaginary); double arg = FastMath.atan2(imaginary, real); return result.set(FastMath.log(modulus), arg); }
@Override public IComplexNumber divi(Number v, IComplexNumber result) { if (this == result) { set(real() / v.doubleValue(), imag()); } else { result.set(result.realComponent().doubleValue() / v.doubleValue(), imaginaryComponent()); } return result; }
/** * Multiply two complex numbers, inplace * * @param c * @param result */ @Override public IComplexNumber muli(IComplexNumber c, IComplexNumber result) { double newR = (real() * c.realComponent().doubleValue() - imag() * c.imaginaryComponent().doubleValue()); double newI = (real() * c.imaginaryComponent().doubleValue() + imag() * c.realComponent().doubleValue()); result.set(newR, newI); return result; }
@Override public IComplexNumber lt(IComplexNumber num) { double val = num.realComponent().doubleValue(); double imag = num.imaginaryComponent().doubleValue(); double otherVal = num.realComponent().doubleValue(); double otherImag = num.imaginaryComponent().doubleValue(); if (val < otherVal) return Nd4j.createComplexNumber(1, 0); else if (val > otherVal) return Nd4j.createComplexNumber(0, 0); else if (imag < otherImag) return Nd4j.createComplexNumber(1, 0); else return Nd4j.createComplexNumber(0, 0); }
/** * Comparing two floatComplex values. * * @param o */ @Override public boolean equals(Object o) { if (!(o instanceof IComplexNumber) || !(o instanceof org.jblas.ComplexFloat) && !(o instanceof org.jblas.ComplexDouble)) { return false; } else { if (o instanceof org.jblas.ComplexFloat) return super.equals(o); else { IComplexNumber num = (IComplexNumber) o; return num.realComponent().doubleValue() == realComponent().doubleValue() && num.imaginaryComponent().doubleValue() == imaginaryComponent().doubleValue(); } } }
@Test public void testCreateComplexFromReal() { INDArray n = Nd4j.create(new double[] {1, 2, 3, 4, 5, 6, 7, 8}, new int[] {2, 4}); IComplexNDArray nComplex = Nd4j.createComplex(n); for (int i = 0; i < n.vectorsAlongDimension(0); i++) { INDArray vec = n.vectorAlongDimension(i, 0); IComplexNDArray vecComplex = nComplex.vectorAlongDimension(i, 0); assertEquals(vec.length(), vecComplex.length()); for (int j = 0; j < vec.length(); j++) { IComplexNumber currComplex = vecComplex.getComplex(j); double curr = vec.getFloat(j); assertEquals(curr, currComplex.realComponent().doubleValue(), 1e-1); } assertEquals(vec, vecComplex.getReal()); } }
private static IComplexNumber sigmoidDeriv(IComplexNumber number) { double arg = number.complexArgument().doubleValue(); double sigArg = 1 / 1 + (FastMath.exp(-arg)) - 1 + .5f; double ret = Math.exp(sigArg); IComplexDouble sigmoid = Nd4j.createDouble(ret, 0); IComplexNumber oneMinus = Nd4j.createComplexNumber(1, 1).subi(sigmoid); return sigmoid.mul(oneMinus); }
/** * Comparing two floatComplex values. * * @param o */ @Override public boolean equals(Object o) { if (!(o instanceof IComplexNumber) || !(o instanceof org.jblas.ComplexFloat) && !(o instanceof org.jblas.ComplexDouble)) { return false; } else { IComplexNumber num = (IComplexNumber) o; double thisReal = realComponent().doubleValue(); double otherReal = num.realComponent().doubleValue(); double thisImag = imaginaryComponent().doubleValue(); double otherImag = imaginaryComponent().doubleValue(); double diff = Math.abs(thisReal - otherReal); double imagDiff = Math.abs(thisImag - otherImag); return diff < Nd4j.EPS_THRESHOLD && imagDiff < Nd4j.EPS_THRESHOLD; } }
@Test public void testBasicOperations() { IComplexNDArray arr = Nd4j.createComplex(new double[] {0, 1, 2, 1, 1, 2, 3, 4}, new int[] {2, 2}); IComplexNumber scalar = arr.sumComplex(); double sum = scalar.realComponent().doubleValue(); assertEquals(6, sum, 1e-1); arr.addi(1); scalar = arr.sumComplex(); sum = scalar.realComponent().doubleValue(); assertEquals(10, sum, 1e-1); arr.subi(Nd4j.createDouble(1, 0)); scalar = arr.sumComplex(); sum = scalar.realComponent().doubleValue(); assertEquals(6, sum, 1e-1); }
/** * Divide two complex numbers, in-place * * @param c * @param result */ @Override public IComplexNumber divi(IComplexNumber c, IComplexNumber result) { float d = c.realComponent().floatValue() * c.realComponent().floatValue() + c.imaginaryComponent().floatValue() * c.imaginaryComponent().floatValue(); float newR = (real() * c.realComponent().floatValue() + imag() * c.imaginaryComponent().floatValue()) / d; float newI = (imag() * c.realComponent().floatValue() - real() * c.imaginaryComponent().floatValue()) / d; result.set(newR, newI); return result; }
private void setMax(Object[] extraArgs) { if (extraArgs.length < 1) throw new IllegalArgumentException("Please specify a max value"); if (Double.isNaN(max.doubleValue()) && extraArgs[0] instanceof Number) { max = Double.valueOf(extraArgs[0].toString()); } else if (Double.isNaN(maxComplex.realComponent().doubleValue()) && extraArgs[0] instanceof IComplexNumber) { maxComplex = (IComplexNumber) extraArgs[0]; } }
@Override public IComplexNumber rdivi(Number v, IComplexNumber result) { float d = result.realComponent().floatValue() * result.realComponent().floatValue() + result.imaginaryComponent().floatValue() * result.imaginaryComponent().floatValue(); return result.set( v.floatValue() * result.realComponent().floatValue() / d, -v.floatValue() * result.imaginaryComponent().floatValue() / d); }
@Test public void testMatrixGet() { IComplexNDArray arr = Nd4j.createComplex((Nd4j.linspace(1, 4, 4))).reshape(2, 2); IComplexNumber n1 = arr.getComplex(0, 0); IComplexNumber n2 = arr.getComplex(0, 1); IComplexNumber n3 = arr.getComplex(1, 0); IComplexNumber n4 = arr.getComplex(1, 1); assertEquals(1, n1.realComponent().doubleValue(), 1e-1); assertEquals(3, n2.realComponent().doubleValue(), 1e-1); assertEquals(2, n3.realComponent().doubleValue(), 1e-1); assertEquals(4, n4.realComponent().doubleValue(), 1e-1); }
@Override public void put(int i, IComplexNumber result) { modified.set(true); if (dataType() == DataBuffer.Type.FLOAT) { JCublas2.cublasSetVector( (int) length(), getElementSize(), PointerUtil.getPointer(CudaComplexConversion.toComplex(result.asFloat())), 1, getHostPointer(), 1); } else { JCublas2.cublasSetVector( (int) length(), getElementSize(), PointerUtil.getPointer(CudaComplexConversion.toComplexDouble(result.asDouble())), 1, getHostPointer(), 1); } }
/** * Subtract two complex numbers, in-place * * @param c * @param result */ @Override public IComplexNumber subi(IComplexNumber c, IComplexNumber result) { if (this == result) { set(real() - c.realComponent().doubleValue(), imag() - c.imaginaryComponent().doubleValue()); } else { result.set( result.realComponent().doubleValue() - c.realComponent().doubleValue(), result.imaginaryComponent().doubleValue() - c.imaginaryComponent().doubleValue()); } return this; }
@Test public void testPutAndGet() { IComplexNDArray multiRow = Nd4j.createComplex(2, 2); multiRow.putScalar(0, 0, Nd4j.createComplexNumber(1, 0)); multiRow.putScalar(0, 1, Nd4j.createComplexNumber(2, 0)); multiRow.putScalar(1, 0, Nd4j.createComplexNumber(3, 0)); multiRow.putScalar(1, 1, Nd4j.createComplexNumber(4, 0)); assertEquals(Nd4j.createComplexNumber(1, 0), multiRow.getComplex(0, 0)); assertEquals(Nd4j.createComplexNumber(2, 0), multiRow.getComplex(0, 1)); assertEquals(Nd4j.createComplexNumber(3, 0), multiRow.getComplex(1, 0)); assertEquals(Nd4j.createComplexNumber(4, 0), multiRow.getComplex(1, 1)); IComplexNDArray arr = Nd4j.createComplex(Nd4j.create(new double[] {1, 2, 3, 4}, new int[] {2, 2})); assertEquals(4, arr.length()); assertEquals(8, arr.data().length()); arr.put(1, 1, Nd4j.scalar(5.0)); IComplexNumber n1 = arr.getComplex(1, 1); IComplexNumber n2 = arr.getComplex(1, 1); assertEquals(getFailureMessage(), 5.0, n1.realComponent().doubleValue(), 1e-1); assertEquals(getFailureMessage(), 0.0, n2.imaginaryComponent().doubleValue(), 1e-1); }
protected void verifyElements(IComplexNDArray d, IComplexNDArray d2) { for (int i = 0; i < d.rows(); i++) { for (int j = 0; j < d.columns(); j++) { IComplexNumber test1 = d.getComplex(i, j); IComplexNumber test2 = d2.getComplex(i, j); assertEquals( test1.realComponent().doubleValue(), test2.realComponent().doubleValue(), 1e-6); assertEquals( test1.imaginaryComponent().doubleValue(), test2.imaginaryComponent().doubleValue(), 1e-6); } } }
@Override public IComplexNumber op(IComplexNumber origin, IComplexNumber other) { if (complexNumber != null) return origin.sub(complexNumber); return complexNumber.sub(num); }
@Override public IComplexNumber rdivi(Number v, IComplexNumber result) { return result.set(v.doubleValue() / real(), imaginaryComponent()); }
@Override public IComplexNumber rdivi(IComplexNumber c, IComplexNumber result) { return result.set(c.div(this)); }
@Override public IComplexNumber set(IComplexNumber set) { return (IComplexNumber) set(set.realComponent().doubleValue(), set.imaginaryComponent().doubleValue()); }
@Override public IComplexNumber rsubi(IComplexNumber a, IComplexNumber result) { return result.set(a.sub(this)); }
@Override public IComplexNumber rsubi(Number a, IComplexNumber result) { return result.set(a.doubleValue() - realComponent().doubleValue(), imaginaryComponent()); }
@Override public IComplexNumber copy(IComplexNumber other) { return other.set(this); }
@Override public void put(long i, IComplexNumber result) { put(i, result.realComponent().doubleValue()); put(i + 1, result.imaginaryComponent().doubleValue()); }
@Override public IComplexNumber getAndSetFinalResult(IComplexNumber accum) { finalResultComplex = accum.div(n()); return finalResultComplex; }