Memory improvements
This commit is contained in:
parent
b1a0e588c1
commit
a81ce84aff
@ -93,7 +93,7 @@ public class ExpressionFunction extends Function {
|
|||||||
|
|
||||||
public float unwrap(Numeric content) {
|
public float unwrap(Numeric content) {
|
||||||
if (content instanceof Real) {
|
if (content instanceof Real) {
|
||||||
return (float) ((Real) content).doubleValue();
|
return (float) content.doubleValue();
|
||||||
}
|
}
|
||||||
if (content instanceof Complex) {
|
if (content instanceof Complex) {
|
||||||
return (float) (imaginary ? ((Complex) content).imaginaryPart() : ((Complex) content).realPart());
|
return (float) (imaginary ? ((Complex) content).imaginaryPart() : ((Complex) content).realPart());
|
||||||
|
@ -512,7 +512,9 @@ public class Expression extends Generic {
|
|||||||
for (int j = 0; j < literal.size(); j++) {
|
for (int j = 0; j < literal.size(); j++) {
|
||||||
final Variable variable = literal.getVariable(j);
|
final Variable variable = literal.getVariable(j);
|
||||||
|
|
||||||
Generic b = content.get(variable).pow(literal.getPower(j));
|
final int power = literal.getPower(j);
|
||||||
|
final Generic contentVariable = content.get(variable);
|
||||||
|
Generic b = pow(contentVariable, power);
|
||||||
|
|
||||||
if (Matrix.isMatrixProduct(sumElement, b)) {
|
if (Matrix.isMatrixProduct(sumElement, b)) {
|
||||||
throw new ArithmeticException("Should not be matrix!");
|
throw new ArithmeticException("Should not be matrix!");
|
||||||
@ -527,6 +529,18 @@ public class Expression extends Generic {
|
|||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
private Generic pow(@Nonnull Generic g, int power) {
|
||||||
|
switch (power) {
|
||||||
|
case 0:
|
||||||
|
return JsclInteger.valueOf(1);
|
||||||
|
case 1:
|
||||||
|
return g;
|
||||||
|
default:
|
||||||
|
return g.pow(power);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Generic expand() {
|
public Generic expand() {
|
||||||
return substitute(literalScm().content(EXPAND_CONVERTER));
|
return substitute(literalScm().content(EXPAND_CONVERTER));
|
||||||
}
|
}
|
||||||
|
@ -46,6 +46,9 @@ public final class JsclInteger extends Generic {
|
|||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public Generic add(@Nonnull Generic that) {
|
public Generic add(@Nonnull Generic that) {
|
||||||
|
if (isZero()) {
|
||||||
|
return that;
|
||||||
|
}
|
||||||
if (that instanceof JsclInteger) {
|
if (that instanceof JsclInteger) {
|
||||||
return add((JsclInteger) that);
|
return add((JsclInteger) that);
|
||||||
} else {
|
} else {
|
||||||
@ -53,8 +56,15 @@ public final class JsclInteger extends Generic {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsclInteger subtract(JsclInteger integer) {
|
private boolean isZero() {
|
||||||
return new JsclInteger(content.subtract(integer.content));
|
return content.equals(ZERO.content);
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsclInteger subtract(JsclInteger that) {
|
||||||
|
if(isZero()) {
|
||||||
|
return that.negate();
|
||||||
|
}
|
||||||
|
return new JsclInteger(content.subtract(that.content));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@ -72,6 +82,9 @@ public final class JsclInteger extends Generic {
|
|||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public Generic multiply(@Nonnull Generic that) {
|
public Generic multiply(@Nonnull Generic that) {
|
||||||
|
if (isOne()) {
|
||||||
|
return that;
|
||||||
|
}
|
||||||
if (that instanceof JsclInteger) {
|
if (that instanceof JsclInteger) {
|
||||||
return multiply((JsclInteger) that);
|
return multiply((JsclInteger) that);
|
||||||
} else {
|
} else {
|
||||||
@ -79,7 +92,14 @@ public final class JsclInteger extends Generic {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isOne() {
|
||||||
|
return content.equals(ONE.content);
|
||||||
|
}
|
||||||
|
|
||||||
public JsclInteger divide(@Nonnull JsclInteger that) {
|
public JsclInteger divide(@Nonnull JsclInteger that) {
|
||||||
|
if (isZero()) {
|
||||||
|
return ZERO;
|
||||||
|
}
|
||||||
JsclInteger e[] = divideAndRemainder(that);
|
JsclInteger e[] = divideAndRemainder(that);
|
||||||
if (e[1].signum() == 0) {
|
if (e[1].signum() == 0) {
|
||||||
return e[0];
|
return e[0];
|
||||||
@ -149,7 +169,7 @@ public final class JsclInteger extends Generic {
|
|||||||
return new JsclInteger(content.pow(exponent));
|
return new JsclInteger(content.pow(exponent));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Generic negate() {
|
public JsclInteger negate() {
|
||||||
return new JsclInteger(content.negate());
|
return new JsclInteger(content.negate());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ import jscl.mathml.MathML;
|
|||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
@ -382,7 +383,7 @@ public class Literal implements Comparable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Map<Variable, Generic> content(@Nonnull Function<Variable, Generic> c) {
|
Map<Variable, Generic> content(@Nonnull Function<Variable, Generic> c) {
|
||||||
final Map<Variable, Generic> result = new TreeMap<Variable, Generic>();
|
final Map<Variable, Generic> result = new HashMap<>(size);
|
||||||
|
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
result.put(variables[i], c.apply(variables[i]));
|
result.put(variables[i], c.apply(variables[i]));
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package jscl.math.function;
|
package jscl.math.function;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
import jscl.CustomFunctionCalculationException;
|
import jscl.CustomFunctionCalculationException;
|
||||||
import jscl.JsclMathEngine;
|
import jscl.JsclMathEngine;
|
||||||
import jscl.math.*;
|
import jscl.math.*;
|
||||||
@ -15,18 +16,17 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||||||
|
|
||||||
public class CustomFunction extends Function implements IFunction {
|
public class CustomFunction extends Function implements IFunction {
|
||||||
|
|
||||||
private static final String LOCAL_VAR_POSTFIX = "_lv_09_03_1988_";
|
|
||||||
|
|
||||||
private final static AtomicInteger counter = new AtomicInteger(0);
|
private final static AtomicInteger counter = new AtomicInteger(0);
|
||||||
|
|
||||||
@Nonnull
|
private final int id;
|
||||||
private final Integer localVarId;
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private Expression content;
|
private Expression content;
|
||||||
@Nullable
|
@Nullable
|
||||||
private String description;
|
private String description;
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private List<String> parameterNames = Collections.emptyList();
|
private List<String> parameterNames = Collections.emptyList();
|
||||||
|
@Nullable
|
||||||
|
private List<ConstantData> parameterConstants;
|
||||||
|
|
||||||
private CustomFunction(@Nonnull String name,
|
private CustomFunction(@Nonnull String name,
|
||||||
@Nonnull List<String> parameterNames,
|
@Nonnull List<String> parameterNames,
|
||||||
@ -36,7 +36,18 @@ public class CustomFunction extends Function implements IFunction {
|
|||||||
this.parameterNames = parameterNames;
|
this.parameterNames = parameterNames;
|
||||||
this.content = content;
|
this.content = content;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.localVarId = counter.incrementAndGet();
|
this.id = counter.incrementAndGet();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
private List<ConstantData> makeParameterConstants(@Nonnull List<String> names) {
|
||||||
|
return new ArrayList<>(Lists.transform(names, new com.google.common.base.Function<String, ConstantData>() {
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public ConstantData apply(@Nullable String name) {
|
||||||
|
return new ConstantData(name);
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
private CustomFunction(@Nonnull String name,
|
private CustomFunction(@Nonnull String name,
|
||||||
@ -51,7 +62,7 @@ public class CustomFunction extends Function implements IFunction {
|
|||||||
throw new CustomFunctionCalculationException(this, e);
|
throw new CustomFunctionCalculationException(this, e);
|
||||||
}
|
}
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.localVarId = counter.incrementAndGet();
|
this.id = counter.incrementAndGet();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -91,29 +102,27 @@ public class CustomFunction extends Function implements IFunction {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Generic selfExpand() {
|
public Generic selfExpand() {
|
||||||
Generic localContent = content;
|
Generic content = this.content;
|
||||||
|
final List<ConstantData> parameterConstants = getParameterConstants();
|
||||||
try {
|
for (ConstantData cd : parameterConstants) {
|
||||||
for (String parameterName : parameterNames) {
|
content = content.substitute(cd.local, cd.globalExpression);
|
||||||
localContent = localContent.substitute(new Constant(parameterName), Expression.valueOf(new Constant(getParameterNameForConstant(parameterName))));
|
|
||||||
}
|
}
|
||||||
|
for (int i = 0; i < parameterConstants.size(); i++) {
|
||||||
for (int i = 0; i < parameterNames.size(); i++) {
|
final ConstantData cd = parameterConstants.get(i);
|
||||||
localContent = localContent.substitute(new Constant(getParameterNameForConstant(parameterNames.get(i))), parameters[i]);
|
content = content.substitute(cd.global, parameters[i]);
|
||||||
}
|
}
|
||||||
|
for (ConstantData cd : parameterConstants) {
|
||||||
} finally {
|
content = content.substitute(cd.global, cd.localExpression);
|
||||||
for (String parameterName : parameterNames) {
|
|
||||||
localContent = localContent.substitute(new Constant(getParameterNameForConstant(parameterName)), Expression.valueOf(new Constant(parameterName)));
|
|
||||||
}
|
}
|
||||||
}
|
return content;
|
||||||
|
|
||||||
return localContent;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private String getParameterNameForConstant(@Nonnull String parameterName) {
|
private List<ConstantData> getParameterConstants() {
|
||||||
return parameterName + LOCAL_VAR_POSTFIX + "_" + this.localVarId;
|
if(parameterConstants == null) {
|
||||||
|
parameterConstants = makeParameterConstants(parameterNames);
|
||||||
|
}
|
||||||
|
return parameterConstants;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -328,4 +337,22 @@ public class CustomFunction extends Function implements IFunction {
|
|||||||
return customFunction;
|
return customFunction;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final class ConstantData {
|
||||||
|
@Nonnull
|
||||||
|
final Constant global;
|
||||||
|
@Nonnull
|
||||||
|
final Constant local;
|
||||||
|
@Nonnull
|
||||||
|
final Generic globalExpression;
|
||||||
|
@Nonnull
|
||||||
|
final Generic localExpression;
|
||||||
|
|
||||||
|
public ConstantData(@Nonnull String name) {
|
||||||
|
global = new Constant(name + "#" + id);
|
||||||
|
globalExpression = Expression.valueOf(global);
|
||||||
|
local = new Constant(name);
|
||||||
|
localExpression = Expression.valueOf(local);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user