Example usage for java.math BigInteger ONE

List of usage examples for java.math BigInteger ONE

Introduction

In this page you can find the example usage for java.math BigInteger ONE.

Prototype

BigInteger ONE

To view the source code for java.math BigInteger ONE.

Click Source Link

Document

The BigInteger constant one.

Usage

From source file:org.nd4j.linalg.util.BigDecimalMath.java

/**
 * Trigonometric cosine.//from  w ww.j  a v  a2s.  co m
 *
 * @param x The argument in radians.
 * @return cos(x) in the range -1 to 1.
 */
static public BigDecimal cos(final BigDecimal x) {
    if (x.compareTo(BigDecimal.ZERO) < 0) {
        return cos(x.negate());
    } else if (x.compareTo(BigDecimal.ZERO) == 0) {
        return BigDecimal.ONE;
    } else {
        /* reduce modulo 2pi
         */
        BigDecimal res = mod2pi(x);
        double errpi = 0.5 * Math.abs(x.ulp().doubleValue());
        int val = +err2prec(FastMath.PI, errpi);
        MathContext mc = new MathContext(val);
        BigDecimal p = pi(mc);
        mc = new MathContext(x.precision());
        if (res.compareTo(p) > 0) {
            /* pi<x<=2pi: cos(x)= - cos(x-pi)
             */
            return cos(subtractRound(res, p)).negate();
        } else if (res.multiply(new BigDecimal("2")).compareTo(p) > 0) {
            /* pi/2<x<=pi: cos(x)= -cos(pi-x)
             */
            return cos(subtractRound(p, res)).negate();
        } else {
            /* for the range 0<=x<Pi/2 one could use cos(2x)= 1-2*sin^2(x)
             * to split this further, or use the cos up to pi/4 and the sine higher up.
            throw new ProviderException("Unimplemented cosine ") ;
             */
            if (res.multiply(new BigDecimal("4")).compareTo(p) > 0) {
                /* x>pi/4: cos(x) = sin(pi/2-x)
                 */
                return sin(subtractRound(p.divide(new BigDecimal("2")), res));
            } else {
                /* Simple Taylor expansion, sum_{i=0..infinity} (-1)^(..)res^(2i)/(2i)! */
                BigDecimal resul = BigDecimal.ONE;
                /* x^i */
                BigDecimal xpowi = BigDecimal.ONE;
                /* 2i factorial */
                BigInteger ifac = BigInteger.ONE;
                /* The absolute error in the result is the error in x^2/2 which is x times the error in x.
                 */
                double xUlpDbl = 0.5 * res.ulp().doubleValue() * res.doubleValue();
                /* The error in the result is set by the error in x^2/2 itself, xUlpDbl.
                 * We need at most k terms to push x^(2k+1)/(2k+1)! below this value.
                 * x^(2k) < xUlpDbl; (2k)*log(x) < log(xUlpDbl);
                 */
                int k = (int) (Math.log(xUlpDbl) / Math.log(res.doubleValue())) / 2;
                MathContext mcTay = new MathContext(err2prec(1., xUlpDbl / k));
                for (int i = 1;; i++) {
                    /* TBD: at which precision will 2*i-1 or 2*i overflow?
                     */
                    ifac = ifac.multiply(new BigInteger("" + (2 * i - 1)));
                    ifac = ifac.multiply(new BigInteger("" + (2 * i)));
                    xpowi = xpowi.multiply(res).multiply(res).negate();
                    BigDecimal corr = xpowi.divide(new BigDecimal(ifac), mcTay);
                    resul = resul.add(corr);
                    if (corr.abs().doubleValue() < 0.5 * xUlpDbl) {
                        break;
                    }
                }
                /* The error in the result is governed by the error in x itself.
                 */
                mc = new MathContext(err2prec(resul.doubleValue(), xUlpDbl));
                return resul.round(mc);
            }
        }
    }
}

From source file:net.pms.util.Rational.java

/**
 * Returns {@code true} if this {@link Rational} can be expressed as an
 * integer value, {@code false} otherwise.
 *
 * @return {@code true} if this can be expressed as an integer value,
 *         {@code false} otherwise./*  w  w  w  . ja  va  2 s.c o m*/
 */
public boolean isInteger() {
    return !isNaN() && !isInfinite() && BigInteger.ONE.equals(reducedDenominator);
}

From source file:gov.nih.nci.cabig.caaers.domain.Fixtures.java

public static InvestigationalNewDrugType createInvestigationalNewDrugType() {
    return createInvestigationalNewDrugType("test", BigInteger.ONE);
}

From source file:org.estatio.api.Api.java

@ActionSemantics(Of.IDEMPOTENT)
public void putLeaseTermForFixed(
        // start generic fields
        @Named("leaseReference") final String leaseReference,
        @Named("tenantReference") final String tenantReference,
        @Named("unitReference") @Optional final String unitReference,
        @Named("itemSequence") final BigInteger itemSequence, @Named("itemType") final String itemType,
        @Named("itemStatus") final String itemStatus,
        @Named("itemStartDate") @Optional final LocalDate itemStartDate,
        @Named("itemEndDate") @Optional final LocalDate itemEndDate,
        @Named("chargeReference") @Optional final String chargeReference,
        @Named("invoicingFrequency") @Optional final String invoicingFrequency,
        @Named("sequence") final BigInteger sequence, @Named("startDate") @Optional final LocalDate startDate,
        @Named("endDate") @Optional final LocalDate endDate, @Named("status") @Optional final String status,
        @Named("atPath") final String atPath,
        // end generic fields
        @Named("value") @Optional final BigDecimal value) {

    putLeaseItem(leaseReference, tenantReference, unitReference, itemType, BigInteger.ONE, itemStartDate,
            itemEndDate, chargeReference, null, invoicingFrequency, "DIRECT_DEBIT", itemStatus, atPath);
    final LeaseTermForFixed term = (LeaseTermForFixed) putLeaseTerm(leaseReference, unitReference, itemSequence,
            itemType, itemStartDate, startDate, endDate, sequence, status);
    term.setValue(value);//from  w  w w .  j  ava2  s  .  c om
}

From source file:org.nd4j.linalg.util.BigDecimalMath.java

/**
 * The trigonometric tangent./*from   w  w  w .j  ava  2s. co  m*/
 *
 * @param x the argument in radians.
 * @return the tan(x)
 */
static public BigDecimal tan(final BigDecimal x) {
    if (x.compareTo(BigDecimal.ZERO) == 0) {
        return BigDecimal.ZERO;
    } else if (x.compareTo(BigDecimal.ZERO) < 0) {
        return tan(x.negate()).negate();
    } else {
        /* reduce modulo pi
         */
        BigDecimal res = modpi(x);
        /* absolute error in the result is err(x)/cos^2(x) to lowest order
         */
        final double xDbl = res.doubleValue();
        final double xUlpDbl = x.ulp().doubleValue() / 2.;
        final double eps = xUlpDbl / 2. / Math.pow(Math.cos(xDbl), 2.);
        if (xDbl > 0.8) {
            /* tan(x) = 1/cot(x) */
            BigDecimal co = cot(x);
            MathContext mc = new MathContext(err2prec(1. / co.doubleValue(), eps));
            return BigDecimal.ONE.divide(co, mc);
        } else {
            final BigDecimal xhighpr = scalePrec(res, 2);
            final BigDecimal xhighprSq = multiplyRound(xhighpr, xhighpr);
            BigDecimal resul = xhighpr.plus();
            /* x^(2i+1) */
            BigDecimal xpowi = xhighpr;
            Bernoulli b = new Bernoulli();
            /* 2^(2i) */
            BigInteger fourn = new BigInteger("4");
            /* (2i)! */
            BigInteger fac = new BigInteger("2");
            for (int i = 2;; i++) {
                Rational f = b.at(2 * i).abs();
                fourn = fourn.shiftLeft(2);
                fac = fac.multiply(new BigInteger("" + (2 * i))).multiply(new BigInteger("" + (2 * i - 1)));
                f = f.multiply(fourn).multiply(fourn.subtract(BigInteger.ONE)).divide(fac);
                xpowi = multiplyRound(xpowi, xhighprSq);
                BigDecimal c = multiplyRound(xpowi, f);
                resul = resul.add(c);
                if (Math.abs(c.doubleValue()) < 0.1 * eps) {
                    break;
                }
            }
            MathContext mc = new MathContext(err2prec(resul.doubleValue(), eps));
            return resul.round(mc);
        }
    }
}

From source file:org.estatio.api.Api.java

private LeaseTerm putLeaseTerm(final String leaseReference, final String unitReference,
        final BigInteger itemSequence, final String itemType, final LocalDate itemStartDate,
        final LocalDate startDate, final LocalDate endDate, final BigInteger sequence, final String statusStr) {
    final Lease lease = fetchLease(leaseReference);
    final Unit unit;
    if (unitReference != null) {
        unit = units.findUnitByReference(unitReference);
        if (unitReference != null && unit == null) {
            throw new ApplicationException(String.format("Unit with reference %s not found.", unitReference));
        }/*www  . j a  v  a2 s.  co  m*/
    }
    final LeaseItemType leaseItemType = fetchLeaseItemType(itemType);
    final LeaseItem item = lease.findItem(leaseItemType, itemStartDate, itemSequence);
    if (item == null) {
        throw new ApplicationException(
                String.format("LeaseItem with reference %1$s, %2$s, %3$s, %4$s not found.", leaseReference,
                        leaseItemType.toString(), itemStartDate.toString(), itemSequence.toString()));
    }
    LeaseTerm term = item.findTermWithSequence(sequence);
    if (term == null) {
        if (sequence.equals(BigInteger.ONE)) {
            term = item.newTerm(startDate, endDate);
        } else {
            final LeaseTerm previousTerm = item.findTermWithSequence(sequence.subtract(BigInteger.ONE));
            term = previousTerm.createNext(startDate, endDate);
        }
        term.setSequence(sequence);
    }
    term.setStatus(org.estatio.dom.lease.LeaseTermStatus.valueOf(statusStr));
    return term;
}

From source file:org.nd4j.linalg.util.BigDecimalMath.java

/**
 * The trigonometric co-tangent.//from  w ww  . ja v a2 s  . c o  m
 *
 * @param x the argument in radians.
 * @return the cot(x)
 */
static public BigDecimal cot(final BigDecimal x) {
    if (x.compareTo(BigDecimal.ZERO) == 0) {
        throw new ArithmeticException("Cannot take cot of zero " + x.toString());
    } else if (x.compareTo(BigDecimal.ZERO) < 0) {
        return cot(x.negate()).negate();
    } else {
        /* reduce modulo pi
         */
        BigDecimal res = modpi(x);
        /* absolute error in the result is err(x)/sin^2(x) to lowest order
         */
        final double xDbl = res.doubleValue();
        final double xUlpDbl = x.ulp().doubleValue() / 2.;
        final double eps = xUlpDbl / 2. / Math.pow(Math.sin(xDbl), 2.);
        final BigDecimal xhighpr = scalePrec(res, 2);
        final BigDecimal xhighprSq = multiplyRound(xhighpr, xhighpr);
        MathContext mc = new MathContext(err2prec(xhighpr.doubleValue(), eps));
        BigDecimal resul = BigDecimal.ONE.divide(xhighpr, mc);
        /* x^(2i-1) */
        BigDecimal xpowi = xhighpr;
        Bernoulli b = new Bernoulli();
        /* 2^(2i) */
        BigInteger fourn = new BigInteger("4");
        /* (2i)! */
        BigInteger fac = BigInteger.ONE;
        for (int i = 1;; i++) {
            Rational f = b.at(2 * i);
            fac = fac.multiply(new BigInteger("" + (2 * i))).multiply(new BigInteger("" + (2 * i - 1)));
            f = f.multiply(fourn).divide(fac);
            BigDecimal c = multiplyRound(xpowi, f);
            if (i % 2 == 0) {
                resul = resul.add(c);
            } else {
                resul = resul.subtract(c);
            }
            if (Math.abs(c.doubleValue()) < 0.1 * eps) {
                break;
            }
            fourn = fourn.shiftLeft(2);
            xpowi = multiplyRound(xpowi, xhighprSq);
        }
        mc = new MathContext(err2prec(resul.doubleValue(), eps));
        return resul.round(mc);
    }
}

From source file:net.sf.json.TestJSONArray.java

public void testToList_BigInteger() {
    List expected = new ArrayList();
    expected.add(BigInteger.ZERO);
    expected.add(BigInteger.ONE);
    JSONArray jsonArray = JSONArray.fromObject(expected);
    List actual = JSONArray.toList(jsonArray);
    Assertions.assertEquals(expected, actual);
}

From source file:com.cloud.utils.net.NetUtils.java

public static BigInteger countIp6InRange(final String ip6Range) {
    if (ip6Range == null) {
        return null;
    }//from   w  w w .ja  va 2  s.co m
    final String[] ips = ip6Range.split("-");
    final String startIp = ips[0];
    String endIp = ips[0];
    if (ips.length > 1) {
        endIp = ips[1];
    }
    try {
        final BigInteger startInt = convertIPv6AddressToBigInteger(IPv6Address.fromString(startIp));
        final BigInteger endInt = convertIPv6AddressToBigInteger(IPv6Address.fromString(endIp));
        if (endInt != null && startInt != null && startInt.compareTo(endInt) <= 0) {
            return endInt.subtract(startInt).add(BigInteger.ONE);
        }
    } catch (final IllegalArgumentException ex) {
        s_logger.error("Failed to convert a string to an IPv6 address", ex);
    }
    return null;
}

From source file:org.apache.juddi.samples.JuddiAdminService.java

void autoMagicDirectedSSL() throws Exception {
    //1 > 2 >3 >1
    List<Node> uddiNodeList = clerkManager.getClientConfig().getUDDINodeList();

    Transport transport = clerkManager.getTransport("default");
    String authtoken = transport.getUDDISecurityService().getAuthToken(new GetAuthToken("root", "root"))
            .getAuthInfo();//from w  ww. ja  va  2s .c o m

    JUDDIApiPortType juddiApiService = transport.getJUDDIApiService();
    System.out.println("fetching...");

    ReplicationConfiguration replicationNodes = null;
    try {
        replicationNodes = juddiApiService.getReplicationNodes(authtoken);
    } catch (Exception ex) {
        System.out.println("Error getting replication config");
        ex.printStackTrace();
        replicationNodes = new ReplicationConfiguration();

    }
    //if (replicationNodes.getCommunicationGraph() == null) {
    replicationNodes.setCommunicationGraph(new CommunicationGraph());
    //}
    Operator op = new Operator();
    op.setOperatorNodeID("uddi:juddi.apache.org:node1");
    op.setSoapReplicationURL(clerkManager.getClientConfig().getUDDINode("default").getReplicationUrl());
    op.setOperatorStatus(OperatorStatusType.NORMAL);
    op.getContact().add(new Contact());
    op.getContact().get(0).getPersonName().add(new PersonName("bob", "en"));
    op.getContact().get(0).setUseType("admin");
    replicationNodes.getOperator().clear();
    replicationNodes.getOperator().add(op);

    op = new Operator();
    op.setOperatorNodeID("uddi:another.juddi.apache.org:node2");
    op.setSoapReplicationURL(clerkManager.getClientConfig().getUDDINode("uddi:another.juddi.apache.org:node2")
            .getReplicationUrl());
    op.setOperatorStatus(OperatorStatusType.NORMAL);
    op.getContact().add(new Contact());
    op.getContact().get(0).getPersonName().add(new PersonName("mary", "en"));
    op.getContact().get(0).setUseType("admin");
    replicationNodes.getOperator().add(op);
    op = new Operator();

    op.setOperatorNodeID("uddi:yet.another.juddi.apache.org:node3");
    op.setSoapReplicationURL(clerkManager.getClientConfig()
            .getUDDINode("uddi:yet.another.juddi.apache.org:node3").getReplicationUrl());
    op.setOperatorStatus(OperatorStatusType.NORMAL);
    op.getContact().add(new Contact());
    op.getContact().get(0).getPersonName().add(new PersonName("mary", "en"));
    op.getContact().get(0).setUseType("admin");
    replicationNodes.getOperator().add(op);
    replicationNodes.getCommunicationGraph().getNode().clear();
    replicationNodes.getCommunicationGraph().getNode().add("uddi:another.juddi.apache.org:node2");
    replicationNodes.getCommunicationGraph().getNode().add("uddi:juddi.apache.org:node1");
    replicationNodes.getCommunicationGraph().getNode().add("uddi:yet.another.juddi.apache.org:node3");
    replicationNodes.getCommunicationGraph().getEdge().clear();
    Edge e = new CommunicationGraph.Edge();
    e.setMessageSender("uddi:juddi.apache.org:node1");
    e.setMessageReceiver("uddi:another.juddi.apache.org:node2");
    replicationNodes.getCommunicationGraph().getEdge().add(e);

    e = new CommunicationGraph.Edge();
    e.setMessageSender("uddi:another.juddi.apache.org:node2");
    e.setMessageReceiver("uddi:yet.another.juddi.apache.org:node3");
    replicationNodes.getCommunicationGraph().getEdge().add(e);

    e = new CommunicationGraph.Edge();
    e.setMessageSender("uddi:yet.another.juddi.apache.org:node3");
    e.setMessageReceiver("uddi:juddi.apache.org:node1");
    replicationNodes.getCommunicationGraph().getEdge().add(e);

    replicationNodes.setSerialNumber(0L);
    replicationNodes.setTimeOfConfigurationUpdate("");
    replicationNodes.setMaximumTimeToGetChanges(BigInteger.ONE);
    replicationNodes.setMaximumTimeToSyncRegistry(BigInteger.ONE);

    if (replicationNodes.getRegistryContact().getContact() == null) {
        replicationNodes.getRegistryContact().setContact(new Contact());
        replicationNodes.getRegistryContact().getContact().getPersonName().add(new PersonName("unknown", null));
    }

    JAXB.marshal(replicationNodes, System.out);

    juddiApiService.setReplicationNodes(authtoken, replicationNodes);
    System.out.println("Saved to node1");
    TestEquals(replicationNodes, juddiApiService.getReplicationNodes(authtoken));

    transport = clerkManager.getTransport("uddi:another.juddi.apache.org:node2");
    authtoken = transport.getUDDISecurityService().getAuthToken(new GetAuthToken("root", "root")).getAuthInfo();
    juddiApiService = transport.getJUDDIApiService();
    juddiApiService.setReplicationNodes(authtoken, replicationNodes);
    System.out.println("Saved to node2");
    TestEquals(replicationNodes, juddiApiService.getReplicationNodes(authtoken));

    transport = clerkManager.getTransport("uddi:yet.another.juddi.apache.org:node3");
    authtoken = transport.getUDDISecurityService().getAuthToken(new GetAuthToken("root", "root")).getAuthInfo();
    juddiApiService = transport.getJUDDIApiService();
    juddiApiService.setReplicationNodes(authtoken, replicationNodes);
    System.out.println("Saved to node3");
    TestEquals(replicationNodes, juddiApiService.getReplicationNodes(authtoken));
}