Example usage for java.lang Math toRadians

List of usage examples for java.lang Math toRadians

Introduction

In this page you can find the example usage for java.lang Math toRadians.

Prototype

public static double toRadians(double angdeg) 

Source Link

Document

Converts an angle measured in degrees to an approximately equivalent angle measured in radians.

Usage

From source file:org.hoteia.qalingo.core.domain.Store.java

public Double getDistanceFromInKm(String fromLatitude, String fromLongitude) {
    double earthRadius = Constants.EARTH_RADIUS;
    double dLat = Math.toRadians(new Double(getLatitude()) - new Double(fromLatitude));
    double dLng = Math.toRadians(new Double(getLongitude()) - new Double(fromLongitude));
    double sindLat = Math.sin(dLat / 2);
    double sindLng = Math.sin(dLng / 2);
    double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2) * Math.cos(Math.toRadians(new Double(fromLatitude)))
            * Math.cos(Math.toRadians(new Double(getLatitude())));
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double dist = earthRadius * c;
    return dist;//from w  w  w  .  j a  v a2 s.c  om
}

From source file:magma.agent.perception.impl.ServerMessageParser.java

/**
 * Parse object coordinates//from  ww w.  j ava2s.  c  o m
 * 
 * @param node
 * @return
 * @throws PerceptorConversionException
 */
private Vector3D parsePol(SymbolNode node) throws PerceptorConversionException {
    try {
        assert node.childCount() == 4 : "Malformed node: " + node.toString();
        assert node.getChild(0).content().equals("pol") : "Expecting a pol object: " + node.toString();

        float val1 = Float.parseFloat(node.getChild(1).content());
        float val2 = Float.parseFloat(node.getChild(2).content());
        float val3 = Float.parseFloat(node.getChild(3).content());

        return new Vector3D(val1, new Vector3D(Math.toRadians(val2), Math.toRadians(val3)));
    } catch (IndexOutOfBoundsException e) {
        throw new PerceptorConversionException("Malformed node: " + node.toString());
    }
}

From source file:com.udojava.evalex.Expression.java

/**
 * Creates a new expression instance from an expression string with a given
 * default match context.//from  www . j a v a 2  s. co  m
 *
 * @param expression The expression. E.g. <code>"2.4*sin(3)/(2-4)"</code> or
 *                   <code>"sin(y)>0 & max(z, 3)>3"</code>
 */
public Expression(String expression, LinkedList<String> hist, Variables vars) {
    this.history = hist;
    this.expression = expression;

    mainVars = vars;

    addOperator(new Operator("+", 20, true, "Addition") {
        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            if (v1.type == ValueType.ARRAY) {
                MyComplex vo = new MyComplex(v1.list);
                vo.list.add(v2);
                return vo;
            }
            return v1.add(v2);
        }
    });

    addOperator(new Operator("-", 20, true, "Subtraction") {
        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            if (v1.type == ValueType.ARRAY) {
                MyComplex vo = new MyComplex(v1.list);
                vo.list.removeIf(o -> o.equals(v2));
                return vo;
            }
            return v1.subtract(v2);
        }
    });
    addOperator(new Operator("*", 30, true, "Real number multiplication") {
        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            return v1.multiply(v2);
        }
    });
    addOperator(new Operator("/", 30, true, "Real number division") {
        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            return v1.divide(v2);
        }
    });
    addOperator(new Operator("%", 30, true, "Remainder of integer division") {
        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            double r = v1.real % v2.real;
            return new MyComplex(r);
        }
    });
    addOperator(
            new Operator("^", 40, false, "Exponentation. See: https://en.wikipedia.org/wiki/Exponentiation") {
                @Override
                public MyComplex eval(MyComplex v1, MyComplex v2) {
                    return v1.pow(v2);
                }
            });
    addOperator(new Operator("&&", 4, false, "Logical AND. Evaluates to 1 if both operands are not 0") {
        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            boolean b1 = (v1.real == 0.0 && v2.real == 0.0);
            return new MyComplex(b1 ? 1 : 0);
        }
    });

    addOperator(new Operator("||", 2, false, "Logical OR. Evaluates to 0 if both operands are 0") {
        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            boolean b1 = (v1.real == 0.0 && v2.real == 0.0);
            return new MyComplex(b1 ? 0 : 1);
        }
    });

    addOperator(new Operator(">", 10, false,
            "Greater than. See: See: https://en.wikipedia.org/wiki/Inequality_(mathematics)") {
        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            if (v1.type == ValueType.REAL && v2.type == ValueType.REAL) {
                return new MyComplex(v1.real > v2.real ? 1 : 0);
            } else {
                return new MyComplex(v1.abs() > v2.abs() ? 1 : 0);
            }
        }
    });

    addOperator(new Operator(">=", 10, false, "Greater or equal") {
        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            if (v1.type == ValueType.REAL && v2.type == ValueType.REAL) {
                return new MyComplex(v1.real >= v2.real ? 1 : 0);
            } else {
                return new MyComplex(v1.abs() >= v2.abs() ? 1 : 0);
            }
        }
    });

    addOperator(new Operator("<", 10, false,
            "Less than. See: https://en.wikipedia.org/wiki/Inequality_(mathematics)") {
        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            if (v1.type == ValueType.REAL && v2.type == ValueType.REAL) {
                return new MyComplex(v1.real < v2.real ? 1 : 0);
            } else {
                return new MyComplex(v1.abs() < v2.abs() ? 1 : 0);
            }
        }
    });

    addOperator(new Operator("<=", 10, false, "less or equal") {
        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            if (v1.type == ValueType.REAL && v2.type == ValueType.REAL) {
                return new MyComplex(v1.real <= v2.real ? 1 : 0);
            } else {
                return new MyComplex(v1.abs() <= v2.abs() ? 1 : 0);
            }
        }
    });

    addOperator(new Operator("->", 7, false, "Set variable v to new value ") {
        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            if (v1 instanceof PitDecimal) {
                PitDecimal target = (PitDecimal) v1;
                String s = target.getVarToken();
                setVariable(s, v2);
                return v2;
            }
            throw new ExpressionException("LHS not variable");
        }
    });

    addOperator(new Operator("=", 7, false, "Equality") {
        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            if (v1.type == ValueType.REAL && v2.type == ValueType.REAL) {
                return new MyComplex(v1.real == v2.real ? 1 : 0);
            } else {
                return new MyComplex(v1.abs() == v2.abs() ? 1 : 0);
            }
        }
    });

    addOperator(new Operator("!=", 7, false,
            "Inequality. See: https://en.wikipedia.org/wiki/Inequality_(mathematics)") {
        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            if (v1.type == ValueType.REAL && v2.type == ValueType.REAL) {
                return new MyComplex(v1.real != v2.real ? 1 : 0);
            } else {
                return new MyComplex(v1.abs() != v2.abs() ? 1 : 0);
            }
        }
    });
    addOperator(
            new Operator("or", 7, false, "Bitwise OR. See: https://en.wikipedia.org/wiki/Logical_disjunction") {
                @Override
                public MyComplex eval(MyComplex v1, MyComplex v2) {
                    return new MyComplex((long) v1.real | (long) v2.real);
                }
            });
    addOperator(new Operator("and", 7, false,
            "Bitwise AND. See: https://en.wikipedia.org/wiki/Logical_conjunction") {
        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            return new MyComplex((long) v1.real & (long) v2.real);
        }
    });
    addOperator(new Operator("xor", 7, false, "Bitwise XOR, See: https://en.wikipedia.org/wiki/Exclusive_or") {
        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            return new MyComplex((long) v1.real ^ (long) v2.real);
        }
    });

    addOperator(new Operator("!", 50, true, "Factorial. See https://en.wikipedia.org/wiki/Factorial") {
        public BigInteger factorial(long n) {
            BigInteger factorial = BigInteger.ONE;
            for (long i = 1; i <= n; i++) {
                factorial = factorial.multiply(BigInteger.valueOf(i));
            }
            return factorial;
        }

        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            BigInteger fact = factorial((long) v1.real);
            return new MyComplex(fact, BigInteger.ZERO);
        }
    });

    addOperator(new Operator("~", 8, false, "Bitwise negation") {
        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            BigInteger bi = v2.toBigIntegerReal();
            int c = bi.bitLength();
            if (c == 0) {
                return new MyComplex(1);
            }
            for (int s = 0; s < c; s++) {
                bi = bi.flipBit(s);
            }
            return new MyComplex(bi);
        }
    });

    addOperator(new Operator("shl", 8, false, "Left Bit shift") {
        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            return new MyComplex((long) v1.real << (long) v2.real);
        }
    });

    addOperator(new Operator("shr", 8, false, "Right bit shift") {
        @Override
        public MyComplex eval(MyComplex v1, MyComplex v2) {
            return new MyComplex((long) v1.real >>> (long) v2.real);
        }
    });

    addFunction(new Function("NOT", 1, "evaluates to 0 if argument != 0") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            boolean zero = parameters.get(0).abs() == 0;
            return new MyComplex(zero ? 1 : 0);
        }
    });

    addFunction(new Function("RND", 2, "Give random number in the range between first and second argument") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            double low = parameters.get(0).real;
            double high = parameters.get(1).real;
            return new MyComplex(low + Math.random() * (high - low));
        }
    });

    MersenneTwister mers = new MersenneTwister(System.nanoTime());

    addFunction(new Function("MRS", 0, "Mersenne twister random generator") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            return new MyComplex(mers.nextDouble());
        }
    });

    addFunction(new Function("BIN", 2, "Binomial Coefficient 'n choose k'") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            int n = (int) parameters.get(0).real;
            int k = (int) parameters.get(1).real;
            double d = CombinatoricsUtils.binomialCoefficientDouble(n, k);
            return new MyComplex(d);
        }
    });
    addFunction(new Function("STIR", 2,
            "Stirling number of 2nd kind: http://mathworld.wolfram.com/StirlingNumberoftheSecondKind.html") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            int n = (int) parameters.get(0).real;
            int k = (int) parameters.get(1).real;
            double d = CombinatoricsUtils.stirlingS2(n, k);
            return new MyComplex(d);
        }
    });

    addFunction(new Function("SIN", 1, "Sine function") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            return parameters.get(0).sin();
        }
    });
    addFunction(new Function("COS", 1, "Cosine function") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            return parameters.get(0).cos();
        }
    });
    addFunction(new Function("TAN", 1, "Tangent") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            return parameters.get(0).tan();
        }
    });
    addFunction(new Function("ASIN", 1, "Reverse Sine") { // added by av
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            return parameters.get(0).asin();
        }
    });
    addFunction(new Function("ACOS", 1, "Reverse Cosine") { // added by av
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            return parameters.get(0).acos();
        }
    });
    addFunction(new Function("ATAN", 1, "Reverse Tangent") { // added by av
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            return parameters.get(0).atan();
        }
    });
    addFunction(new Function("SINH", 1, "Hyperbolic Sine") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            return parameters.get(0).sinh();
        }
    });
    addFunction(new Function("COSH", 1, "Hyperbolic Cosine") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            return parameters.get(0).cosh();
        }
    });
    addFunction(new Function("TANH", 1, "Hyperbolic Tangent") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            return parameters.get(0).tanh();
        }
    });
    addFunction(new Function("RAD", 1, "Transform degree to radian") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            double d = Math.toRadians(parameters.get(0).real);
            return new MyComplex(d);
        }
    });
    addFunction(new Function("DEG", 1, "Transform radian to degree") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            double d = Math.toDegrees(parameters.get(0).real);
            return new MyComplex(d);
        }
    });
    addFunction(new Function("MAX", -1, "Find the biggest value in a list") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            MyComplex save = new MyComplex(Double.MIN_VALUE);
            if (parameters.size() == 0) {
                throw new ExpressionException("MAX requires at least one parameter");
            }
            //                if (parameters.get(0).type == ValueType.ARRAY)
            //                    parameters = parameters.get(0).list;
            if (parameters.get(0).type == ValueType.COMPLEX) {
                for (MyComplex parameter : parameters) {
                    if (parameter.abs() > save.abs()) {
                        save = parameter;
                    }
                }
                save.type = ValueType.COMPLEX;
            } else {
                for (MyComplex parameter : parameters) {
                    if (parameter.real > save.real) {
                        save = parameter;
                    }
                }
                save.type = ValueType.REAL;
            }
            return save;
        }
    });
    ///////////////////////////////////////////////////////
    addFunction(new Function("IF", 3, "Conditional: give param3 if param1 is 0, otherwise param2") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            if (parameters.get(0).real == 0.0) {
                return parameters.get(2);
            }
            return parameters.get(1);
        }
    });

    addFunction(new Function("PERC", 2, "Get param1 percent of param2") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            return parameters.get(0).divide(new MyComplex(100)).multiply(parameters.get(1));
        }
    });

    addFunction(new Function("PER", 2, "How many percent is param1 of param2") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            return parameters.get(0).multiply(new MyComplex(100)).divide(parameters.get(1));
        }
    });

    addFunction(new Function("H", 1, "Evaluate _history element") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            int i = (int) parameters.get(0).real;
            Expression ex = new Expression(history.get(i), history, mainVars);
            return ex.eval();
        }
    });

    addFunction(new Function("MERS", 1, "Calculate Mersenne Number") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            MyComplex p = parameters.get(0);
            return new MyComplex(2).pow(p).subtract(new MyComplex(1));
        }
    });

    addFunction(new Function("GCD", 2, "Find greatest common divisor of 2 values") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            double a = parameters.get(0).real;
            double b = parameters.get(1).real;
            long r = ArithmeticUtils.gcd((long) a, (long) b);
            return new MyComplex(r);
        }
    });
    addFunction(new Function("LCM", 2, "Find least common multiple of 2 values") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            double a = parameters.get(0).real;
            double b = parameters.get(1).real;
            long r = ArithmeticUtils.lcm((long) a, (long) b);
            return new MyComplex(r);
        }
    });
    addFunction(new Function("AMEAN", -1, "Arithmetic mean of a set of values") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            if (parameters.size() == 0) {
                throw new ExpressionException("MEAN requires at least one parameter");
            }
            Mean m = new Mean();
            double[] d = MyComplex.getRealArray(parameters);
            double d2 = m.evaluate(d);
            return new MyComplex(d2);
        }
    });
    //        addFunction(new Function("BYT", -1,
    //                "Value from sequence of bytes")
    //        {
    //            @Override
    //            public MyComplex eval (List<MyComplex> parameters)
    //            {
    //                if (parameters.size() == 0)
    //                {
    //                    return MyComplex.ZERO;
    //                }
    //                BigInteger res = BigInteger.ZERO;
    //                for (MyComplex parameter : parameters)
    //                {
    //                    if (parameter.intValue() < 0 || parameter.intValue() > 255)
    //                    {
    //                        throw new ExpressionException("not a byte value");
    //                    }
    //                    res = res.shiftLeft(8);
    //                    res = res.or(parameter.toBigInteger());
    //                }
    //                return new MyComplex(res, BigInteger.ZERO);
    //            }
    //        });
    addFunction(new Function("SEQ", 3, "Generate Sequence p1=start, p2=step, p3=count") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            double start = parameters.get(0).real;
            ArrayList<MyComplex> arr = new ArrayList<>();
            for (int s = 0; s < (int) (parameters.get(2).real); s++) {
                arr.add(new MyComplex(start));
                start += parameters.get(1).real;
            }
            return new MyComplex(arr);
        }
    });

    addFunction(new Function("PROD", -1, "Product of real values") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            Product p = new Product();
            double[] d = MyComplex.getRealArray(parameters);
            return new MyComplex(p.evaluate(d));
        }
    });

    addFunction(new Function("SUM", -1, "Sum of values") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            Sum p = new Sum();
            double[] d = MyComplex.getRealArray(parameters);
            return new MyComplex(p.evaluate(d));
        }
    });

    addFunction(new Function("ANG", 1, "Angle phi of complex number in radians") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            double b = parameters.get(0).angle();
            return new MyComplex(b);
        }
    });

    addFunction(new Function("IM", 1, "Get imaginary part") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            return new MyComplex(parameters.get(0).imaginary);
        }
    });

    addFunction(new Function("RE", 1, "Get real part") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            return new MyComplex(parameters.get(0).real);
        }
    });

    addFunction(new Function("POL", 2, "Make complex number from polar coords. angle is first arg") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            double angle = parameters.get(0).real;
            double len = parameters.get(1).real;
            Complex c = ComplexUtils.polar2Complex(len, angle);
            return new MyComplex(c);
        }
    });

    addFunction(new Function("GMEAN", -1, "Geometric mean of a set of values") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            if (parameters.size() == 0) {
                throw new ExpressionException("MEAN requires at least one parameter");
            }
            GeometricMean m = new GeometricMean();
            double[] d = MyComplex.getRealArray(parameters);
            double d2 = m.evaluate(d);
            return new MyComplex(d2);
        }
    });

    addFunction(new Function("HMEAN", -1, "Harmonic mean of a set of values") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            if (parameters.size() == 0) {
                throw new ExpressionException("MEAN requires at least one parameter");
            }
            MyComplex res = new MyComplex(0);
            int num = 0;
            for (MyComplex parameter : parameters) {
                res = res.add(new MyComplex(1).divide(parameter));
                num++;
            }
            res = new MyComplex(res.abs());
            return new MyComplex(num).divide(res);
        }
    });

    addFunction(new Function("VAR", -1, "Variance of a set of values") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            if (parameters.size() == 0) {
                throw new ExpressionException("MEAN requires at least one parameter");
            }
            double[] arr = new double[parameters.size()];
            for (int s = 0; s < parameters.size(); s++) {
                arr[s] = parameters.get(s).real;
            }
            return new MyComplex(variance(arr));
        }
    });

    addFunction(new Function("NPR", 1, "Next prime number greater or equal the argument") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            return new MyComplex(nextPrime((int) parameters.get(0).real));
        }
    });

    addFunction(new Function("NSWP", 1, "Swap nibbles") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            BigInteger bi = parameters.get(0).toBigIntegerReal();
            String s = bi.toString(16);
            s = new StringBuilder(s).reverse().toString();
            return new MyComplex(new BigInteger(s, 16), BigInteger.ZERO);
        }
    });

    addFunction(new Function("BSWP", 1, "Swap bytes") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            BigInteger bi = parameters.get(0).toBigIntegerReal();
            String s = bi.toString(16);
            while (s.length() % 4 != 0) {
                s = s + "0";
            }
            if (bi.intValue() < 256) {
                s = "00" + s;
            }
            s = Misc.reverseHex(s);
            return new MyComplex(new BigInteger(s, 16), BigInteger.ZERO);
        }
    });

    addFunction(new Function("PYT", 2,
            "Pythagoras's result = sqrt(param1^2+param2^2) https://en.wikipedia.org/wiki/Pythagorean_theorem") {
        @Override
        public MyComplex eval(List<MyComplex> par) {
            double a = par.get(0).real;
            double b = par.get(1).real;
            return new MyComplex(Math.sqrt(a * a + b * b));
        }
    });

    addFunction(new Function("FIB", 1, "Fibonacci number") {
        // --Commented out by Inspection (2/19/2017 7:46 PM):private final Operator exp = operators.get("^");

        @Override
        public MyComplex eval(List<MyComplex> par) {
            return Misc.iterativeFibonacci((int) par.get(0).real);
        }
    });

    ///////////////////////////////////////////////

    addFunction(new Function("MIN", -1, "Find the smallest in a list of values") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            MyComplex save = new MyComplex(Double.MAX_VALUE);
            if (parameters.size() == 0) {
                throw new ExpressionException("MAX requires at least one parameter");
            }
            if (parameters.get(0).type == ValueType.COMPLEX) {
                for (MyComplex parameter : parameters) {
                    if (parameter.abs() < save.abs()) {
                        save = parameter;
                    }
                }
                save.type = ValueType.COMPLEX;
            } else {
                for (MyComplex parameter : parameters) {
                    if (parameter.real < save.real) {
                        save = parameter;
                    }
                }
                save.type = ValueType.REAL;
            }
            return save;
        }
    });
    addFunction(new Function("ABS", 1, "Get absolute value of a number") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            return new MyComplex(parameters.get(0).abs());
        }
    });
    addFunction(new Function("LN", 1, "Logarithm base e of the argument") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            double d = Math.log(parameters.get(0).real);
            return new MyComplex(d);
        }
    });
    addFunction(new Function("LOG", 1, "Logarithm base 10 of the argument") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            double d = Math.log10(parameters.get(0).real);
            return new MyComplex(d);
        }
    });
    addFunction(new Function("FLOOR", 1, "Rounds DOWN to nearest Integer") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            double d = Math.floor(parameters.get(0).real);
            return new MyComplex(d);
        }
    });
    addFunction(new Function("CEIL", 1, "Rounds UP to nearest Integer") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            double d = Math.ceil(parameters.get(0).real);
            return new MyComplex(d);
        }
    });
    addFunction(new Function("ROU", 1, "Rounds to nearest Integer") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            int d = (int) (parameters.get(0).real + 0.5);
            return new MyComplex(d);
        }
    });
    addFunction(new Function("SQRT", 1, "Square root") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            MyComplex p = parameters.get(0);
            if (p.type == ValueType.REAL) {
                return new MyComplex(Math.sqrt(p.real));
            }
            return p.sqrt();
        }
    });
    addFunction(new Function("ARR", -1, "Create array") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            return new MyComplex(parameters);
        }
    });
    addFunction(new Function("POLY", -1, "Treat array as Polynom") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            double[] d = MyComplex.getRealArray(parameters);
            PolynomialFunction p = new PolynomialFunction(d);
            return new MyComplex(p);
        }
    });
    addFunction(new Function("DRVE", -1, "Make derivative of polynomial") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            PolynomialFunction p;
            if (parameters.get(0).isPoly()) {
                p = new PolynomialFunction(parameters.get(0).getRealArray());
            } else {
                double[] d = MyComplex.getRealArray(parameters);
                p = new PolynomialFunction(d);
            }
            return new MyComplex(p.polynomialDerivative());
        }
    });
    addFunction(new Function("ADRVE", -1, "Make antiderivative of polynomial. Constant is always zero") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            PolynomialFunction p;
            if (parameters.get(0).isPoly()) {
                p = new PolynomialFunction(parameters.get(0).getRealArray());
            } else {
                double[] d = MyComplex.getRealArray(parameters);
                p = new PolynomialFunction(d);
            }
            return new MyComplex(Misc.antiDerive(p));
        }
    });

    addFunction(new Function("PVAL", 2, "Compute value of polynom for the given argument.") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            if (parameters.get(0).isPoly()) {
                PolynomialFunction p = new PolynomialFunction(parameters.get(0).getRealArray());
                double v = p.value(parameters.get(1).real);
                return new MyComplex(v);
            }
            throw new ExpressionException("first arg must be polynomial");
        }
    });

    addFunction(new Function("INTGR", 3, "Numerical integration") {
        @Override
        public MyComplex eval(List<MyComplex> parameters) {
            if (parameters.get(0).isPoly()) {
                PolynomialFunction p = new PolynomialFunction(parameters.get(0).getRealArray());
                double start = parameters.get(1).real;
                double end = parameters.get(2).real;
                SimpsonIntegrator si = new SimpsonIntegrator();
                double d = si.integrate(1000, p, start, end);
                return new MyComplex(d);
            }
            throw new ExpressionException("first arg must be polynomial");
        }
    });

}

From source file:uk.ac.diamond.scisoft.xpdf.views.XPDFPhase.java

/**
 * Changes the present unit cell from rhombohedral to hexagonal.
 * <p> /*from   w  ww.j  a v  a 2s.  c  om*/
 * Changes the unit cell parameters from a rhombohedral basis (aaa, )
 * to a hexagonal basis (aac 90,90,120), and sets the space
 * group to the hexagonal equivalent.
 */
private void unitCelltoHexagonal() {
    setSpaceGroup(spaceGroup.asHexagonal());

    double ar = unitCellLengths[0];
    double cosAlpha = Math.cos(Math.toRadians(unitCellDegrees[0]));
    // Convert to the squared length
    ar *= ar;
    // Calculate the squared unit cell lengths
    double ah = 2 * ar * (1 - cosAlpha);
    double c = 3 * ar * (1 + 2 * cosAlpha);
    ah = Math.sqrt(ah);
    c = Math.sqrt(c);
    unitCellLengths[0] = unitCellLengths[1] = ah;
    unitCellLengths[2] = c;
    unitCellDegrees[0] = unitCellDegrees[1] = 90.0;
    unitCellDegrees[2] = 120;
}

From source file:org.jlab.clas.swimtools.Swim.java

/**
 * /*w w w  .j  av a 2s  .co  m*/
 * @param d_cm
 * @param n
 * @param dir
 * @return return state  x,y,z,px,py,pz, pathlength, iBdl at the plane surface in the lab frame
 */
public double[] SwimToPlaneBoundary(double d_cm, Vector3D n, int dir) {

    double[] value = new double[8];
    if (this.SwimUnPhys)
        return null;
    double d = d_cm / 100;

    double hdata[] = new double[3];
    // using adaptive stepsize

    // the new swim to plane in swimmer
    Plane plane = new Plane(n.x(), n.y(), n.z(), d);
    SwimTrajectory st;
    try {

        // swim backwards?
        double vertexR = Math.sqrt(_x0 * _x0 + _y0 * _y0 + _z0 * _z0);
        if ((vertexR > d) && (dir > 0)) {

            //trying to swim forward, but already beyond the plane. Just return the starting values
            double thetaRad = Math.toRadians(_theta);
            double phiRad = Math.toRadians(_phi);
            double pz = _pTot * Math.cos(thetaRad);
            double pperp = _pTot * Math.sin(thetaRad);
            double px = pperp * Math.cos(phiRad);
            double py = pperp * Math.sin(phiRad);

            value[0] = _x0 * 100; // convert back to cm
            value[1] = _y0 * 100; // convert back to cm
            value[2] = _z0 * 100; // convert back to cm

            value[3] = px;
            value[4] = py;
            value[5] = pz;
            value[6] = 0;
            value[7] = 0;

            return value;

        }

        else {
            st = PC.CF.swim(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, plane, accuracy, _maxPathLength,
                    stepSize, cnuphys.swim.Swimmer.CLAS_Tolerance, hdata);

            st.computeBDL(PC.CP);

            double[] lastY = st.lastElement();

            value[0] = lastY[0] * 100; // convert back to cm
            value[1] = lastY[1] * 100; // convert back to cm
            value[2] = lastY[2] * 100; // convert back to cm
            value[3] = lastY[3] * _pTot; // normalized values
            value[4] = lastY[4] * _pTot;
            value[5] = lastY[5] * _pTot;
            value[6] = lastY[6] * 100;
            value[7] = lastY[7] * 10; // Conversion from kG.m to T.cm

            // System.out.println("\nCOMPARE plane swims DIRECTION = " +
            // dir);
            // for (int i = 0; i < 8; i++) {
            // System.out.print(String.format("%-8.5f ", value[i]));
            // }

        }
    } catch (RungeKuttaException e) {
        e.printStackTrace();
    }

    //      PlaneBoundarySwimStopper stopper = new PlaneBoundarySwimStopper(d, n, dir);

    // this is a uniform stepsize swimmer (dph)
    //      st = PC.CF.swim(_charge, _x0, _y0, _z0, _pTot, _theta, _phi, stopper, _maxPathLength, stepSize, 0.0005);
    //      st.computeBDL(PC.CP);
    //      // st.computeBDL(compositeField);
    //
    //      double[] lastY = st.lastElement();
    //
    //      value[0] = lastY[0] * 100; // convert back to cm
    //      value[1] = lastY[1] * 100; // convert back to cm
    //      value[2] = lastY[2] * 100; // convert back to cm
    //      value[3] = lastY[3] * _pTot; // normalized values
    //      value[4] = lastY[4] * _pTot;
    //      value[5] = lastY[5] * _pTot;
    //      value[6] = lastY[6] * 100;
    //      value[7] = lastY[7] * 10; // Conversion from kG.m to T.cm
    //
    //      double tv1 = Math.abs(tvalue[0]);
    //      double v1 = Math.abs(value[0]);
    //
    //      double fract = (Math.abs(tv1 - v1) / Math.max(tv1, v1));
    //      if (fract > 0.9) {
    //         System.out.println("\nBig Diff fract = " + fract + "   direction = " + dir + "   DIST = " + d);
    //
    //         double vtxR = Math.sqrt(_x0 * _x0 + _y0 * _y0 + _z0 * _z0);
    //         System.out.println("VTX: (" + _x0 + ", " + _y0 + ", " + _z0 + ") VtxR: " + vtxR + "   P: " + _pTot
    //               + "  theta: " + _theta + "  phi: " + _phi);
    //
    //         printV("tV", tvalue);
    //         printV(" V", value);
    //         // System.out.println("tV: (" + tvalue[0]/100 + ", " + tvalue[1]/100
    //         // + ", " + tvalue[2]/100 + ")");
    //         // System.out.println(" V: (" + value[0]/100 + ", " + value[1]/100 +
    //         // ", " + value[2]/100 + ")");
    //      }
    //
    //      // System.out.println();
    //      // for (int i = 0; i < 8; i++) {
    //      // System.out.print(String.format("%-8.5f ", value[i]));
    //      // }
    //      // System.out.println();

    return value;

}

From source file:org.pentaho.platform.uifoundation.chart.FlashChartComponent.java

private String setRadialTicks(final double x_center, final double y_center, final double radius,
        final double length, final double start_angle, final double end_angle, final long ticks_count,
        final long thickness, final String color) {
    StringBuffer sb = new StringBuffer();
    for (double i = start_angle; i <= end_angle; i += (end_angle - start_angle) / (ticks_count - 1)) {
        sb.append("<line x1='").append(x_center + Math.sin(Math.toRadians(i)) * radius).append("' y1='") //$NON-NLS-1$//$NON-NLS-2$
                .append(y_center - Math.cos(Math.toRadians(i)) * radius).append("'"); //$NON-NLS-1$
        sb.append(" x2='").append(x_center + Math.sin(Math.toRadians(i)) * (radius + length)).append("' y2='") //$NON-NLS-1$//$NON-NLS-2$
                .append(y_center - Math.cos(Math.toRadians(i)) * (radius + length)).append("'"); //$NON-NLS-1$
        sb.append(" thickness='").append(thickness).append("' color='").append(color).append("' />\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    }/*from   w w w . j av  a 2  s  . c  om*/
    return (sb.toString());
}

From source file:business.ImageManager.java

public ImageIcon getArrow(double angle) {
    BufferedImage img = new BufferedImage(40, 40, BufferedImage.TRANSLUCENT);
    Graphics2D big = img.createGraphics();
    //setup para os rastros
    big.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    big.setStroke(//from ww w . j ava  2  s.  c  om
            new BasicStroke(2f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1f, new float[] { 5f }, 0f));
    big.setColor(Color.red);

    int cx = this.getYellowBall().getIconWidth() / 2;
    int cy = this.getYellowBall().getIconHeight() / 2;
    AffineTransform at = AffineTransform.getTranslateInstance(cx, cy);
    at.rotate(Math.toRadians(angle));
    //        at.scale(2.0, 2.0);
    Shape shape = at.createTransformedShape(createArrow());
    big.setPaint(Color.red);
    big.draw(shape);
    ImageIcon ret = new ImageIcon(img);
    return (ret);
    //        tenta com o icone...angle.
}

From source file:com.facebook.presto.operator.scalar.MathFunctions.java

@Description("converts an angle in degrees to radians")
@ScalarFunction//  w  w w  . ja v a  2  s.c  o  m
@SqlType(StandardTypes.DOUBLE)
public static double radians(@SqlType(StandardTypes.DOUBLE) double degrees) {
    return Math.toRadians(degrees);
}

From source file:com.skumar.flexibleciruclarseekbar.CircularSeekBar.java

/**
 * Convert from cartesian to polar// ww  w . j a v a2  s .c  o m
 *
 * @param xPos current x point
 * @param yPos current y point
 * @return angle of the Thumb w.r.t the arc.
 */
private double getTouchDegrees(float xPos, float yPos) {
    float x = xPos - mTranslateX;
    float y = yPos - mTranslateY;
    //invert the x-coord if we are rotating anti-clockwise
    x = (mClockwise) ? x : -x;
    // convert to arc Angle
    double angle = Math.toDegrees(Math.atan2(y, x) + (Math.PI / 2) - Math.toRadians(mRotation));
    if (angle < 0) {
        angle = 360 + angle;
    }
    angle -= mStartAngle;
    return angle;
}

From source file:org.eclipse.birt.chart.device.swt.SwtRendererImpl.java

/**
 * Extra fix due to SWT arc rendering limitation.
 * //  w ww. j a v  a  2  s . co m
 * @param _gc
 * @param are
 * @param dTranslateX
 * @param dTranslateY
 * @param dScale
 */
protected void drawArc(GC _gc, Device _dv, ArcRenderEvent are, double dTranslateX, double dTranslateY,
        double dScale) {

    if (are.getInnerRadius() >= 0 && (are.getOuterRadius() > 0 && are.getInnerRadius() < are.getOuterRadius())
            || (are.getInnerRadius() > 0 && are.getOuterRadius() <= 0)) {
        Bounds bo = goFactory.createBounds(are.getTopLeft().getX(), are.getTopLeft().getY(), are.getWidth(),
                are.getHeight());

        Bounds rctOuter, rctInner;

        rctOuter = getOuterRectangle(are, dTranslateX, dTranslateY, dScale, bo);

        rctInner = getInnerRectangle(are, dTranslateX, dTranslateY, dScale, bo);

        double startAngle = Math.toRadians(-are.getStartAngle());
        double stopAngle = Math.toRadians(-are.getStartAngle() - are.getAngleExtent());

        double xsOuter = (rctOuter.getLeft() + (Math.cos(startAngle) * 0.5 + 0.5) * rctOuter.getWidth());
        double ysOuter = (rctOuter.getTop() + (Math.sin(startAngle) * 0.5 + 0.5) * rctOuter.getHeight());

        // double xeOuter = ( rctOuter.getLeft( ) + ( Math.cos( stopAngle )
        // * 0.5 + 0.5 )
        // * rctOuter.getWidth( ) );
        // double yeOuter = ( rctOuter.getTop( ) + ( Math.sin( stopAngle ) *
        // 0.5 + 0.5 )
        // * rctOuter.getHeight( ) );
        //
        // double xsInner = ( rctInner.getLeft( ) + ( Math.cos( startAngle )
        // * 0.5 + 0.5 )
        // * rctInner.getWidth( ) );
        // double ysInner = ( rctInner.getTop( ) + ( Math.sin( startAngle )
        // * 0.5 + 0.5 )
        // * rctInner.getHeight( ) );

        double xeInner = (rctInner.getLeft() + (Math.cos(stopAngle) * 0.5 + 0.5) * rctInner.getWidth());
        double yeInner = (rctInner.getTop() + (Math.sin(stopAngle) * 0.5 + 0.5) * rctInner.getHeight());

        Path pt = new Path(_dv);
        pt.addArc((float) rctOuter.getLeft(), (float) rctOuter.getTop(), (float) rctOuter.getWidth(),
                (float) rctOuter.getHeight(), (float) are.getStartAngle(), (float) are.getAngleExtent());

        pt.lineTo((float) xeInner, (float) yeInner);

        pt.addArc((float) rctInner.getLeft(), (float) rctInner.getTop(), (float) rctInner.getWidth(),
                (float) rctInner.getHeight(), (float) (are.getStartAngle() + are.getAngleExtent()),
                (float) -are.getAngleExtent());

        pt.lineTo((float) xsOuter, (float) ysOuter);

        _gc.drawPath(pt);

        pt.dispose();
    } else {
        if (are.getStyle() == ArcRenderEvent.OPEN) {
            _gc.drawArc((int) ((are.getTopLeft().getX() + dTranslateX) * dScale),
                    (int) ((are.getTopLeft().getY() + dTranslateY) * dScale), (int) (are.getWidth() * dScale),
                    (int) (are.getHeight() * dScale), (int) are.getStartAngle(), (int) are.getAngleExtent());
        } else {
            double xc = ((are.getTopLeft().getX() + dTranslateX + are.getWidth() / 2d) * dScale);
            double yc = ((are.getTopLeft().getY() + dTranslateY + are.getHeight() / 2d) * dScale);

            double xs = 0, ys = 0, xe = 0, ye = 0;

            double angle = Math.toRadians(-are.getStartAngle());

            xs = ((are.getTopLeft().getX() + dTranslateX + (Math.cos(angle) * 0.5 + 0.5) * are.getWidth())
                    * dScale);
            ys = ((are.getTopLeft().getY() + dTranslateY + (Math.sin(angle) * 0.5 + 0.5) * are.getHeight())
                    * dScale);

            angle = Math.toRadians(-are.getStartAngle() - are.getAngleExtent());

            xe = ((are.getTopLeft().getX() + dTranslateX + (Math.cos(angle) * 0.5 + 0.5) * are.getWidth())
                    * dScale);
            ye = ((are.getTopLeft().getY() + dTranslateY + (Math.sin(angle) * 0.5 + 0.5) * are.getHeight())
                    * dScale);

            Path pt = new Path(_dv);
            if (are.getStyle() == ArcRenderEvent.CLOSED) {
                pt.addArc((float) ((are.getTopLeft().getX() + dTranslateX) * dScale),
                        (float) ((are.getTopLeft().getY() + dTranslateY) * dScale),
                        (float) (are.getWidth() * dScale), (float) (are.getHeight() * dScale),
                        (float) are.getStartAngle(), (float) are.getAngleExtent());
                // fix in case angle extent is zero.
                pt.moveTo((float) xe, (float) ye);
                pt.lineTo((float) xs, (float) ys);

                _gc.drawPath(pt);
            } else if (are.getStyle() == ArcRenderEvent.SECTOR) {
                pt.addArc((float) ((are.getTopLeft().getX() + dTranslateX) * dScale),
                        (float) ((are.getTopLeft().getY() + dTranslateY) * dScale),
                        (float) (are.getWidth() * dScale), (float) (are.getHeight() * dScale),
                        (float) are.getStartAngle(), (float) are.getAngleExtent());
                // fix in case angle extent is zero.
                pt.moveTo((float) xe, (float) ye);
                pt.lineTo((float) xc, (float) yc);
                pt.lineTo((float) xs, (float) ys);

                _gc.drawPath(pt);
            }
            pt.dispose();
        }
    }

}