Example usage for org.apache.commons.math3.util ArithmeticUtils factorial

List of usage examples for org.apache.commons.math3.util ArithmeticUtils factorial

Introduction

In this page you can find the example usage for org.apache.commons.math3.util ArithmeticUtils factorial.

Prototype

public static long factorial(final int n) throws NotPositiveException, MathArithmeticException 

Source Link

Document

Returns n!.

Usage

From source file:cc.redberry.physics.oneloopdiv.Averaging.java

@Override
public Tensor transform(Tensor tensor) {
    if (tensor instanceof Sum || tensor instanceof Expression) {
        int i;/*from   ww  w.j a va 2s.c o m*/
        Tensor tensorCurrent, tempResult;
        Tensor[] newSumElements = new Tensor[tensor.size()];
        boolean needRebuild = false;
        for (i = tensor.size() - 1; i >= 0; --i) {
            tensorCurrent = tensor.get(i);
            tempResult = transform(tensorCurrent);
            if (tensorCurrent != tempResult)
                needRebuild = true;
            newSumElements[i] = tempResult;
        }
        if (needRebuild)
            return tensor.getFactory().create(newSumElements);
        else
            return tensor;
    }

    if (tensor instanceof Product) {
        int i;
        int count = 0;
        Tensor current, old;
        IndicesBuilder ib = new IndicesBuilder();
        List<Tensor> newProductElements = new ArrayList<>();
        for (i = tensor.size() - 1; i >= 0; --i) {
            current = tensor.get(i);
            if (isN(current)) {
                ib.append(current);
                ++count;
            } else {
                if (TensorUtils.isScalar(current)) {
                    FromChildToParentIterator iterator = new FromChildToParentIterator(current);
                    Tensor temp;
                    boolean flag = false;
                    while ((temp = iterator.next()) != null) {
                        if (isN(temp)) {
                            flag = true;
                            break;
                        }
                    }
                    if (!flag) {
                        newProductElements.add(current);
                        continue;
                    }
                    if (!(current instanceof Power) || !TensorUtils.isInteger(current.get(1))
                            || ((Complex) current.get(1)).intValue() != 2)
                        throw new IllegalArgumentException();

                    Tensor[] bases = { current.get(0), current.get(0) };
                    bases[1] = ApplyIndexMapping.renameDummy(bases[1],
                            TensorUtils.getAllIndicesNamesT(tensor).toArray());
                    flag = false;
                    for (Tensor base : bases) {
                        for (Tensor t : base) {
                            if (isN(t)) {
                                ib.append(t);
                                ++count;
                                flag = true;
                            } else
                                newProductElements.add(t);
                        }
                    }
                    if (!flag)
                        throw new IllegalArgumentException("Expand first");
                } else
                    newProductElements.add(current);
            }
        }
        if (count == 0)
            return tensor;
        if (count % 2 != 0)
            return Complex.ZERO;
        //            System.out.println(count);
        count = count / 2;
        Tensor result = average(ib.getIndices().getAllIndices().copy());
        long factor = ArithmeticUtils.pow((long) 2, count) * ArithmeticUtils.factorial(count + 1);//may be BigInteger?
        Complex number = new Complex((long) factor).reciprocal();
        result = ExpandTransformation.expand(result);
        newProductElements.add(number);
        newProductElements.add(result);
        return Tensors.multiply(newProductElements.toArray(new Tensor[newProductElements.size()]));
    }

    if (tensor instanceof Power) {
        Tensor nBase = transform(tensor.get(0));
        if (nBase == tensor.get(0))
            return tensor;
        return Tensors.pow(nBase, tensor.get(1));
    }
    return tensor;
}

From source file:de.tuberlin.uebb.jbop.example.DSCompilerOnlyCompose.java

@Override
public double taylor(final double[] ds, final double... delta) {
    double value = 0;
    for (int i = sizes[parameters][order] - 1; i >= 0; --i) {
        final int[] orders = derivativesIndirection[i];
        double term = ds[i];
        for (int k = 0; k < orders.length; ++k) {
            if (orders[k] > 0) {
                term *= FastMath.pow(delta[k], orders[k]) / ArithmeticUtils.factorial(orders[k]);
            }//from w w  w  .  ja  v  a  2 s  . c om
        }
        value += term;
    }
    return value;
}

From source file:de.tuberlin.uebb.jbop.example.DSCompiler.java

@Override
@Optimizable/*from   w ww . ja  va2  s . c o  m*/
@StrictLoops
public double taylor(final double[] ds, final double... delta) {
    double value = 0;
    for (int i = sizes[parameters][order] - 1; i >= 0; --i) {
        final int[] orders = derivativesIndirection[i];
        double term = ds[i];
        for (int k = 0; k < orders.length; ++k) {
            if (orders[k] > 0) {
                term *= FastMath.pow(delta[k], orders[k]) / ArithmeticUtils.factorial(orders[k]);
            }
        }
        value += term;
    }
    return value;
}