Skip to content

Commit

Permalink
Merge pull request #7 from datahaki/v093
Browse files Browse the repository at this point in the history
v093
  • Loading branch information
datahaki authored Apr 26, 2021
2 parents 5080fd6 + 6aafbd7 commit e335802
Show file tree
Hide file tree
Showing 381 changed files with 4,516 additions and 2,399 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ language: java
jdk:
- openjdk8
- openjdk9
- openjdk10
- openjdk11
- openjdk12
- openjdk13
- openjdk14
- openjdk15

notifications:
email: false
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ch.ethz.idsc.tensor <a href="https://travis-ci.org/idsc-frazzoli/tensor"><img src="https://travis-ci.org/idsc-frazzoli/tensor.svg?branch=master" alt="Build Status"></a>

Library for tensor computations in Java, version `0.9.2`
Library for tensor computations in Java, version `0.9.3`

The tensor library was developed with the following objectives in mind
* support for exact precision using integer fractions
Expand Down Expand Up @@ -288,7 +288,7 @@ Specify `repository` and `dependency` of the tensor library in the `pom.xml` fil
<dependency>
<groupId>ch.ethz.idsc</groupId>
<artifactId>tensor</artifactId>
<version>0.9.2</version>
<version>0.9.3</version>
</dependency>
</dependencies>
```
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

<groupId>ch.ethz.idsc</groupId>
<artifactId>tensor</artifactId>
<version>0.9.2</version>
<version>0.9.3</version>
<packaging>jar</packaging>

<name>tensor</name>
<url>http://www.hakenberg.de</url>
<url>https://github.com/datahaki/tensor</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ch/ethz/idsc/tensor/AbstractRealScalar.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public Scalar log() {
if (isNonNegative()) {
double value = number().doubleValue();
if (LOG_LO < value && value < LOG_HI)
return DoubleScalar.of(Math.log1p(subtract(RealScalar.ONE).number().doubleValue()));
return DoubleScalar.of(Math.log1p(subtract(one()).number().doubleValue()));
return DoubleScalar.of(Math.log(value));
}
return ComplexScalarImpl.of(Log.FUNCTION.apply(negate()), Pi.VALUE);
Expand All @@ -97,7 +97,7 @@ public Scalar log() {
public Scalar power(Scalar exponent) {
if (Scalars.isZero(this)) {
if (Scalars.isZero(exponent))
return ONE;
return ONE; // Mathematica evaluates 0^0 as Indeterminate
if (exponent instanceof ComplexEmbedding) {
ComplexEmbedding complexEmbedding = (ComplexEmbedding) exponent;
if (Sign.isPositive(complexEmbedding.real()))
Expand Down
61 changes: 43 additions & 18 deletions src/main/java/ch/ethz/idsc/tensor/BigDecimalMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
package ch.ethz.idsc.tensor;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.math.RoundingMode;

import ch.ethz.idsc.tensor.ext.Integers;

/** implementation is standalone */
/* package */ enum BigDecimalMath {
;
private static final BigDecimal TWO = BigDecimal.valueOf(2);

/** computation of square-root using Newton iteration
*
* @param square
Expand All @@ -28,23 +28,23 @@ public static BigDecimal sqrt(BigDecimal square, MathContext mathContext) {
BigDecimal xn1 = BigDecimal.ONE;
while (xn0.compareTo(xn1) != 0) {
xn0 = xn1;
BigDecimal fx = xn0.pow(2).subtract(square);
BigDecimal fpx = xn0.multiply(TWO);
BigDecimal fx = xn0.multiply(xn0).subtract(square);
BigDecimal fpx = xn0.add(xn0); // equals to 2 * xn0
xn1 = fx.divide(fpx, mathContext);
xn1 = xn0.subtract(xn1, mathContext);
}
return xn1;
}

/***************************************************/
/** @param x
* @param mathContext
* @return exponential of x */
public static BigDecimal exp(BigDecimal x, MathContext mathContext) {
BigDecimal xn0 = BigDecimal.ZERO;
BigDecimal xn1 = BigDecimal.ONE;
BigDecimal add = x;
int index = 0;
while (xn0.compareTo(xn1) != 0) {
for (int index = 0; xn0.compareTo(xn1) != 0;) {
xn0 = xn1;
add = add.multiply(x).divide(BigDecimal.valueOf(++index), mathContext);
xn1 = xn1.add(add, mathContext);
Expand All @@ -59,10 +59,9 @@ public static BigDecimal sin(BigDecimal x, MathContext mathContext) {
BigDecimal xn0 = BigDecimal.ZERO;
BigDecimal xn1 = x;
BigDecimal add = x;
final BigDecimal x2 = x.multiply(x, mathContext);
BigDecimal x2 = x.multiply(x, mathContext);
int count = 0;
int index = 1;
while (xn0.compareTo(xn1) != 0) {
for (int index = 1; xn0.compareTo(xn1) != 0;) {
xn0 = xn1;
add = add.multiply(x2).divide(BigDecimal.valueOf(++index * ++index), mathContext);
xn1 = Integers.isEven(++count) //
Expand All @@ -79,9 +78,8 @@ public static BigDecimal sinh(BigDecimal x, MathContext mathContext) {
BigDecimal xn0 = BigDecimal.ZERO;
BigDecimal xn1 = x;
BigDecimal add = x;
final BigDecimal x2 = x.multiply(x, mathContext);
int index = 1;
while (xn0.compareTo(xn1) != 0) {
BigDecimal x2 = x.multiply(x, mathContext);
for (int index = 1; xn0.compareTo(xn1) != 0;) {
xn0 = xn1;
add = add.multiply(x2).divide(BigDecimal.valueOf(++index * ++index), mathContext);
xn1 = xn1.add(add, mathContext);
Expand All @@ -96,10 +94,9 @@ public static BigDecimal cos(BigDecimal x, MathContext mathContext) {
BigDecimal xn0 = BigDecimal.ZERO;
BigDecimal xn1 = BigDecimal.ONE;
BigDecimal add = BigDecimal.ONE;
final BigDecimal x2 = x.multiply(x, mathContext);
BigDecimal x2 = x.multiply(x, mathContext);
int count = 0;
int index = 0;
while (xn0.compareTo(xn1) != 0) {
for (int index = 0; xn0.compareTo(xn1) != 0;) {
xn0 = xn1;
add = add.multiply(x2).divide(BigDecimal.valueOf(++index * ++index), mathContext);
xn1 = Integers.isEven(++count) //
Expand All @@ -116,13 +113,41 @@ public static BigDecimal cosh(BigDecimal x, MathContext mathContext) {
BigDecimal xn0 = BigDecimal.ZERO;
BigDecimal xn1 = BigDecimal.ONE;
BigDecimal add = BigDecimal.ONE;
final BigDecimal x2 = x.multiply(x, mathContext);
int index = 0;
while (xn0.compareTo(xn1) != 0) {
BigDecimal x2 = x.multiply(x, mathContext);
for (int index = 0; xn0.compareTo(xn1) != 0;) {
xn0 = xn1;
add = add.multiply(x2).divide(BigDecimal.valueOf(++index * ++index), mathContext);
xn1 = xn1.add(add, mathContext);
}
return xn1;
}

/***************************************************/
/** @param bigDecimal
* @return
* @throws Exception if value is Infinity */
public static BigInteger floor(BigDecimal bigDecimal) {
BigInteger bigInteger = bigDecimal.toBigInteger();
if (0 < new BigDecimal(bigInteger).compareTo(bigDecimal)) {
bigDecimal = bigDecimal.subtract(BigDecimal.ONE);
bigInteger = bigDecimal.toBigInteger();
}
return bigInteger;
}

/** @param bigDecimal
* @return
* @throws Exception if value is Infinity */
public static BigInteger ceiling(BigDecimal bigDecimal) {
BigInteger bigInteger = bigDecimal.toBigInteger();
if (new BigDecimal(bigInteger).compareTo(bigDecimal) < 0) {
bigDecimal = bigDecimal.add(BigDecimal.ONE);
bigInteger = bigDecimal.toBigInteger();
}
return bigInteger;
}

public static BigInteger round(BigDecimal bigDecimal) {
return bigDecimal.setScale(0, RoundingMode.HALF_UP).toBigIntegerExact();
}
}
1 change: 0 additions & 1 deletion src/main/java/ch/ethz/idsc/tensor/BigFraction.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

/** immutable integer fraction in normal form, i.e. denominator is strictly positive */
/* package */ final class BigFraction implements Comparable<BigFraction>, Serializable {
private static final long serialVersionUID = 8490275462897833358L;
private static final String DIVIDE = "/";

/** @param value
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/ch/ethz/idsc/tensor/ComplexHelper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// code by jph
package ch.ethz.idsc.tensor;

import ch.ethz.idsc.tensor.nrm.Hypot;
import ch.ethz.idsc.tensor.sca.Abs;
import ch.ethz.idsc.tensor.sca.Arg;
import ch.ethz.idsc.tensor.sca.Sign;
Expand Down Expand Up @@ -50,18 +51,18 @@ public static Scalar c_dcd(Scalar c, Scalar d) {
*
* @param c
* @param d non-zero
* @return */
* @return Sqrt[c + d*i] */
public static Scalar sqrt(Scalar c, Scalar d) {
Scalar ca = Abs.FUNCTION.apply(c);
Scalar da = Abs.FUNCTION.apply(d);
final Scalar w;
if (Scalars.lessEquals(ca, da)) {
Scalar c_d = c.divide(d);
Scalar fraction = Abs.FUNCTION.apply(c_d).add(Sqrt.FUNCTION.apply(RealScalar.ONE.add(c_d.multiply(c_d)))).multiply(RationalScalar.HALF);
Scalar fraction = Abs.FUNCTION.apply(c_d).add(Hypot.withOne(c_d)).multiply(RationalScalar.HALF);
w = Sqrt.FUNCTION.apply(da).multiply(Sqrt.FUNCTION.apply(fraction));
} else {
Scalar d_c = d.divide(c);
Scalar fraction = RealScalar.ONE.add(Sqrt.FUNCTION.apply(RealScalar.ONE.add(d_c.multiply(d_c)))).multiply(RationalScalar.HALF);
Scalar fraction = RealScalar.ONE.add(Hypot.withOne(d_c)).multiply(RationalScalar.HALF);
w = Sqrt.FUNCTION.apply(ca).multiply(Sqrt.FUNCTION.apply(fraction));
}
if (Sign.isPositiveOrZero(c))
Expand Down
1 change: 0 additions & 1 deletion src/main/java/ch/ethz/idsc/tensor/ComplexScalarImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

/* package */ class ComplexScalarImpl extends AbstractScalar implements ComplexScalar, //
ChopInterface, ExactScalarQInterface, MachineNumberQInterface, NInterface, Serializable {
private static final long serialVersionUID = 3023231904329254618L;
private static final BinaryPower<Scalar> BINARY_POWER = new BinaryPower<>(ScalarProduct.INSTANCE);

/** creator with package visibility
Expand Down
Loading

0 comments on commit e335802

Please sign in to comment.