BigReal.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.math4.legacy.linear;


  18. import java.io.Serializable;
  19. import java.math.BigDecimal;
  20. import java.math.BigInteger;
  21. import java.math.MathContext;
  22. import java.math.RoundingMode;

  23. import org.apache.commons.math4.legacy.core.Field;
  24. import org.apache.commons.math4.legacy.core.FieldElement;
  25. import org.apache.commons.math4.legacy.exception.MathArithmeticException;
  26. import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;

  27. /**
  28.  * Arbitrary precision decimal number.
  29.  * <p>
  30.  * This class is a simple wrapper around the standard <code>BigDecimal</code>
  31.  * in order to implement the {@link FieldElement} interface.
  32.  * </p>
  33.  * @since 2.0
  34.  */
  35. public class BigReal implements FieldElement<BigReal>, Comparable<BigReal>, Serializable {

  36.     /** A big real representing 0. */
  37.     public static final BigReal ZERO = new BigReal(BigDecimal.ZERO);

  38.     /** A big real representing 1. */
  39.     public static final BigReal ONE = new BigReal(BigDecimal.ONE);

  40.     /** Serializable version identifier. */
  41.     private static final long serialVersionUID = 4984534880991310382L;

  42.     /** Underlying BigDecimal. */
  43.     private final BigDecimal d;

  44.     /** Rounding mode for divisions. */
  45.     private RoundingMode roundingMode = RoundingMode.HALF_UP;

  46.     /** BigDecimal scale. */
  47.     private int scale = 64;

  48.     /** Build an instance from a BigDecimal.
  49.      * @param val value of the instance
  50.      */
  51.     public BigReal(BigDecimal val) {
  52.         d =  val;
  53.     }

  54.     /** Build an instance from a BigInteger.
  55.      * @param val value of the instance
  56.      */
  57.     public BigReal(BigInteger val) {
  58.         d = new BigDecimal(val);
  59.     }

  60.     /** Build an instance from an unscaled BigInteger.
  61.      * @param unscaledVal unscaled value
  62.      * @param scale scale to use
  63.      */
  64.     public BigReal(BigInteger unscaledVal, int scale) {
  65.         d = new BigDecimal(unscaledVal, scale);
  66.     }

  67.     /** Build an instance from an unscaled BigInteger.
  68.      * @param unscaledVal unscaled value
  69.      * @param scale scale to use
  70.      * @param mc to used
  71.      */
  72.     public BigReal(BigInteger unscaledVal, int scale, MathContext mc) {
  73.         d = new BigDecimal(unscaledVal, scale, mc);
  74.     }

  75.     /** Build an instance from a BigInteger.
  76.      * @param val value of the instance
  77.      * @param mc context to use
  78.      */
  79.     public BigReal(BigInteger val, MathContext mc) {
  80.         d = new BigDecimal(val, mc);
  81.     }

  82.     /** Build an instance from a characters representation.
  83.      * @param in character representation of the value
  84.      */
  85.     public BigReal(char[] in) {
  86.         d = new BigDecimal(in);
  87.     }

  88.     /** Build an instance from a characters representation.
  89.      * @param in character representation of the value
  90.      * @param offset offset of the first character to analyze
  91.      * @param len length of the array slice to analyze
  92.      */
  93.     public BigReal(char[] in, int offset, int len) {
  94.         d = new BigDecimal(in, offset, len);
  95.     }

  96.     /** Build an instance from a characters representation.
  97.      * @param in character representation of the value
  98.      * @param offset offset of the first character to analyze
  99.      * @param len length of the array slice to analyze
  100.      * @param mc context to use
  101.      */
  102.     public BigReal(char[] in, int offset, int len, MathContext mc) {
  103.         d = new BigDecimal(in, offset, len, mc);
  104.     }

  105.     /** Build an instance from a characters representation.
  106.      * @param in character representation of the value
  107.      * @param mc context to use
  108.      */
  109.     public BigReal(char[] in, MathContext mc) {
  110.         d = new BigDecimal(in, mc);
  111.     }

  112.     /** Build an instance from a double.
  113.      * @param val value of the instance
  114.      */
  115.     public BigReal(double val) {
  116.         d = new BigDecimal(val);
  117.     }

  118.     /** Build an instance from a double.
  119.      * @param val value of the instance
  120.      * @param mc context to use
  121.      */
  122.     public BigReal(double val, MathContext mc) {
  123.         d = new BigDecimal(val, mc);
  124.     }

  125.     /** Build an instance from an int.
  126.      * @param val value of the instance
  127.      */
  128.     public BigReal(int val) {
  129.         d = new BigDecimal(val);
  130.     }

  131.     /** Build an instance from an int.
  132.      * @param val value of the instance
  133.      * @param mc context to use
  134.      */
  135.     public BigReal(int val, MathContext mc) {
  136.         d = new BigDecimal(val, mc);
  137.     }

  138.     /** Build an instance from a long.
  139.      * @param val value of the instance
  140.      */
  141.     public BigReal(long val) {
  142.         d = new BigDecimal(val);
  143.     }

  144.     /** Build an instance from a long.
  145.      * @param val value of the instance
  146.      * @param mc context to use
  147.      */
  148.     public BigReal(long val, MathContext mc) {
  149.         d = new BigDecimal(val, mc);
  150.     }

  151.     /** Build an instance from a String representation.
  152.      * @param val character representation of the value
  153.      */
  154.     public BigReal(String val) {
  155.         d = new BigDecimal(val);
  156.     }

  157.     /** Build an instance from a String representation.
  158.      * @param val character representation of the value
  159.      * @param mc context to use
  160.      */
  161.     public BigReal(String val, MathContext mc)  {
  162.         d = new BigDecimal(val, mc);
  163.     }

  164.     /***
  165.      * Gets the rounding mode for division operations.
  166.      * The default is {@code RoundingMode.HALF_UP}
  167.      * @return the rounding mode.
  168.      * @since 2.1
  169.      */
  170.     public RoundingMode getRoundingMode() {
  171.         return roundingMode;
  172.     }

  173.     /***
  174.      * Sets the rounding mode for decimal divisions.
  175.      * @param roundingMode rounding mode for decimal divisions
  176.      * @since 2.1
  177.      */
  178.     public void setRoundingMode(RoundingMode roundingMode) {
  179.         this.roundingMode = roundingMode;
  180.     }

  181.     /***
  182.      * Sets the scale for division operations.
  183.      * The default is 64
  184.      * @return the scale
  185.      * @since 2.1
  186.      */
  187.     public int getScale() {
  188.         return scale;
  189.     }

  190.     /***
  191.      * Sets the scale for division operations.
  192.      * @param scale scale for division operations
  193.      * @since 2.1
  194.      */
  195.     public void setScale(int scale) {
  196.         this.scale = scale;
  197.     }

  198.     /** {@inheritDoc} */
  199.     @Override
  200.     public BigReal add(BigReal a) {
  201.         return new BigReal(d.add(a.d));
  202.     }

  203.     /** {@inheritDoc} */
  204.     @Override
  205.     public BigReal subtract(BigReal a) {
  206.         return new BigReal(d.subtract(a.d));
  207.     }

  208.     /** {@inheritDoc} */
  209.     @Override
  210.     public BigReal negate() {
  211.         return new BigReal(d.negate());
  212.     }

  213.     /**
  214.      * {@inheritDoc}
  215.      *
  216.      * @throws MathArithmeticException if {@code a} is zero
  217.      */
  218.     @Override
  219.     public BigReal divide(BigReal a) throws MathArithmeticException {
  220.         try {
  221.             return new BigReal(d.divide(a.d, scale, roundingMode));
  222.         } catch (ArithmeticException e) {
  223.             // Division by zero has occurred
  224.             throw new MathArithmeticException(LocalizedFormats.ZERO_NOT_ALLOWED);
  225.         }
  226.     }

  227.     /**
  228.      * {@inheritDoc}
  229.      *
  230.      * @throws MathArithmeticException if {@code this} is zero
  231.      */
  232.     @Override
  233.     public BigReal reciprocal() throws MathArithmeticException {
  234.         try {
  235.             return new BigReal(BigDecimal.ONE.divide(d, scale, roundingMode));
  236.         } catch (ArithmeticException e) {
  237.             // Division by zero has occurred
  238.             throw new MathArithmeticException(LocalizedFormats.ZERO_NOT_ALLOWED);
  239.         }
  240.     }

  241.     /** {@inheritDoc} */
  242.     @Override
  243.     public BigReal multiply(BigReal a) {
  244.         return new BigReal(d.multiply(a.d));
  245.     }

  246.     /** {@inheritDoc} */
  247.     @Override
  248.     public BigReal multiply(final int n) {
  249.         return new BigReal(d.multiply(new BigDecimal(n)));
  250.     }

  251.     /** {@inheritDoc} */
  252.     @Override
  253.     public int compareTo(BigReal a) {
  254.         return d.compareTo(a.d);
  255.     }

  256.     /** Get the double value corresponding to the instance.
  257.      * @return double value corresponding to the instance
  258.      */
  259.     public double doubleValue() {
  260.         return d.doubleValue();
  261.     }

  262.     /** Get the BigDecimal value corresponding to the instance.
  263.      * @return BigDecimal value corresponding to the instance
  264.      */
  265.     public BigDecimal bigDecimalValue() {
  266.         return d;
  267.     }

  268.     /** {@inheritDoc} */
  269.     @Override
  270.     public boolean equals(Object other) {
  271.         if (this == other){
  272.             return true;
  273.         }

  274.         if (other instanceof BigReal) {
  275.             return d.compareTo(((BigReal) other).d) == 0;
  276.         }
  277.         return false;
  278.     }

  279.     /** {@inheritDoc} */
  280.     @Override
  281.     public int hashCode() {
  282.         return Double.hashCode(d.doubleValue());
  283.     }

  284.     /** {@inheritDoc} */
  285.     @Override
  286.     public Field<BigReal> getField() {
  287.         return BigRealField.getInstance();
  288.     }
  289. }