Method for evaluating math expressions in Java

There’s also exp4j, an expression evaluator based on Dijkstra’s Shunting Yard. It’s freely available and redistributable under the Apache License 2.0, only about 25KB in size, and quite easy to use: Calculable calc = new ExpressionBuilder(“3 * sin(y) – 2 / (x – 2)”) .withVariable(“x”, varX) .withVariable(“y”, varY) .build() double result1=calc.calculate(); When using a newer … Read more

Formula with dynamic number of variables

See ?as.formula, e.g.: factors <- c(“factor1”, “factor2”) as.formula(paste(“y~”, paste(factors, collapse=”+”))) # y ~ factor1 + factor2 where factors is a character vector containing the names of the factors you want to use in the model. This you can paste into an lm model, e.g.: set.seed(0) y <- rnorm(100) factor1 <- rep(1:2, each=50) factor2 <- rep(3:4, … Read more

Dynamically evaluate an expression from a formula in Pandas

You can use 1) pd.eval(), 2) df.query(), or 3) df.eval(). Their various features and functionality are discussed below. Examples will involve these dataframes (unless otherwise specified). np.random.seed(0) df1 = pd.DataFrame(np.random.choice(10, (5, 4)), columns=list(‘ABCD’)) df2 = pd.DataFrame(np.random.choice(10, (5, 4)), columns=list(‘ABCD’)) df3 = pd.DataFrame(np.random.choice(10, (5, 4)), columns=list(‘ABCD’)) df4 = pd.DataFrame(np.random.choice(10, (5, 4)), columns=list(‘ABCD’)) 1) pandas.eval This is … Read more

JAVA Looping a formula

I’m not 100% sure what you are trying to achive. But assuming you are trying to do some sort of compounding interest. This should help. public static void main(String[] args) { double startAmount = 1000; double ratePercentage = 0.05; double newAmount; newAmount = startAmount; for (int i = 0; i <= 10; i++){ newAmount = … Read more

Formula to find week numbers from the Total [closed]

You could take a bitwise AND & with the day value and take this day. const days = { Monday: 1, Tuesday: 2, Wednesday: 4, Thursday: 8, Friday: 16, Saturday: 32, Sunday: 64 }, getDays = value => Object.keys(days).filter(day => value & days[day]); console.log(getDays(127)); // All Days console.log(getDays(80)); // Friday, Sunday console.log(getDays(7)); // Monday, Tuesday, … Read more