Delay math registry initialization

This commit is contained in:
Sergey Solovyev 2017-05-25 16:50:25 +02:00
parent b6945f17e7
commit fd0dc88c64
13 changed files with 275 additions and 239 deletions

View File

@ -37,7 +37,5 @@ public interface EntitiesRegistry<E extends MathEntity> extends MathRegistry<E>
@Nullable @Nullable
Category getCategory(@Nonnull E entity); Category getCategory(@Nonnull E entity);
void init();
void save(); void save();
} }

View File

@ -23,9 +23,9 @@
package org.solovyev.android.calculator; package org.solovyev.android.calculator;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import jscl.JsclMathEngine;
import jscl.math.function.IConstant;
import org.simpleframework.xml.Serializer; import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister; import org.simpleframework.xml.core.Persister;
import org.solovyev.android.Check; import org.solovyev.android.Check;
@ -39,12 +39,16 @@ import org.solovyev.android.calculator.variables.OldVars;
import org.solovyev.android.calculator.variables.VariableCategory; import org.solovyev.android.calculator.variables.VariableCategory;
import org.solovyev.android.io.FileSaver; import org.solovyev.android.io.FileSaver;
import java.io.File;
import java.util.List;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import java.io.File;
import java.util.List; import jscl.JsclMathEngine;
import jscl.math.function.IConstant;
@Singleton @Singleton
public class VariablesRegistry extends BaseEntitiesRegistry<IConstant> { public class VariablesRegistry extends BaseEntitiesRegistry<IConstant> {
@ -82,23 +86,20 @@ public class VariablesRegistry extends BaseEntitiesRegistry<IConstant> {
bus.post(new RemovedEvent(variable)); bus.post(new RemovedEvent(variable));
} }
public void init() { @Override
protected void onInit() {
Check.isNotMainThread(); Check.isNotMainThread();
try { migrateOldVariables();
migrateOldVariables();
for (CppVariable variable : loadEntities(CppVariable.JSON_CREATOR)) { for (CppVariable variable : loadEntities(CppVariable.JSON_CREATOR)) {
addSafely(variable.toJsclConstant()); addSafely(variable.toJsclConstant());
}
addSafely("x");
addSafely("y");
addSafely("t");
addSafely("j");
} finally {
setInitialized();
} }
addSafely("x");
addSafely("y");
addSafely("t");
addSafely("j");
} }
private void migrateOldVariables() { private void migrateOldVariables() {

View File

@ -27,7 +27,9 @@ import android.content.SharedPreferences;
import android.os.Handler; import android.os.Handler;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.solovyev.android.Check; import org.solovyev.android.Check;
@ -41,14 +43,19 @@ import org.solovyev.android.io.FileSystem;
import org.solovyev.common.math.MathEntity; import org.solovyev.common.math.MathEntity;
import org.solovyev.common.math.MathRegistry; import org.solovyev.common.math.MathRegistry;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.Executor;
public abstract class BaseEntitiesRegistry<T extends MathEntity> implements EntitiesRegistry<T> { public abstract class BaseEntitiesRegistry<T extends MathEntity> implements EntitiesRegistry<T> {
@ -103,8 +110,16 @@ public abstract class BaseEntitiesRegistry<T extends MathEntity> implements Enti
} }
@Override @Override
public void init() { public final void init() {
setInitialized(); try {
mathRegistry.init();
onInit();
} finally {
setInitialized();
}
}
protected void onInit() {
} }
@NonNull @NonNull
@ -121,7 +136,7 @@ public abstract class BaseEntitiesRegistry<T extends MathEntity> implements Enti
return Collections.emptyList(); return Collections.emptyList();
} }
protected final void setInitialized() { private final void setInitialized() {
synchronized (lock) { synchronized (lock) {
Check.isTrue(!initialized); Check.isTrue(!initialized);
initialized = true; initialized = true;

View File

@ -22,11 +22,10 @@
package org.solovyev.android.calculator.functions; package org.solovyev.android.calculator.functions;
import static android.text.TextUtils.isEmpty;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import jscl.JsclMathEngine;
import jscl.math.function.CustomFunction;
import jscl.math.function.Function;
import jscl.math.function.IFunction;
import org.simpleframework.xml.Serializer; import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister; import org.simpleframework.xml.core.Persister;
import org.solovyev.android.Check; import org.solovyev.android.Check;
@ -39,14 +38,22 @@ import org.solovyev.android.calculator.json.Jsonable;
import org.solovyev.android.io.FileSaver; import org.solovyev.android.io.FileSaver;
import org.solovyev.common.text.Strings; import org.solovyev.common.text.Strings;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import java.io.File;
import java.util.*;
import static android.text.TextUtils.isEmpty; import jscl.JsclMathEngine;
import jscl.math.function.CustomFunction;
import jscl.math.function.Function;
import jscl.math.function.IFunction;
@Singleton @Singleton
public class FunctionsRegistry extends BaseEntitiesRegistry<Function> { public class FunctionsRegistry extends BaseEntitiesRegistry<Function> {
@ -95,26 +102,27 @@ public class FunctionsRegistry extends BaseEntitiesRegistry<Function> {
} }
@Override @Override
public void init() { protected void onInit() {
Check.isNotMainThread(); Check.isNotMainThread();
try { migrateOldFunctions();
migrateOldFunctions();
final List<CustomFunction.Builder> functions = new ArrayList<>(); final List<CustomFunction.Builder> functions = new ArrayList<>();
functions.add(new CustomFunction.Builder(true, "log", Arrays.asList("base", "x"), "ln(x)/ln(base)")); functions.add(new CustomFunction.Builder(true, "log", Arrays.asList("base", "x"),
functions.add(new CustomFunction.Builder(true, "√3", Collections.singletonList("x"), "x^(1/3)")); "ln(x)/ln(base)"));
functions.add(new CustomFunction.Builder(true, "√4", Collections.singletonList("x"), "x^(1/4)")); functions.add(new CustomFunction.Builder(true, "√3", Collections.singletonList("x"),
functions.add(new CustomFunction.Builder(true, "√n", Arrays.asList("x", "n"), "x^(1/n)")); "x^(1/3)"));
functions.add(new CustomFunction.Builder(true, "re", Collections.singletonList("x"), "(x+conjugate(x))/2")); functions.add(new CustomFunction.Builder(true, "√4", Collections.singletonList("x"),
functions.add(new CustomFunction.Builder(true, "im", Collections.singletonList("x"), "(x-conjugate(x))/(2*i)")); "x^(1/4)"));
functions.add(new CustomFunction.Builder(true, "√n", Arrays.asList("x", "n"), "x^(1/n)"));
functions.add(new CustomFunction.Builder(true, "re", Collections.singletonList("x"),
"(x+conjugate(x))/2"));
functions.add(new CustomFunction.Builder(true, "im", Collections.singletonList("x"),
"(x-conjugate(x))/(2*i)"));
for (CppFunction function : loadEntities(CppFunction.JSON_CREATOR)) { for (CppFunction function : loadEntities(CppFunction.JSON_CREATOR)) {
functions.add(function.toJsclBuilder()); functions.add(function.toJsclBuilder());
}
addSafely(functions);
} finally {
setInitialized();
} }
addSafely(functions);
} }
/** /**

View File

@ -1,34 +1,33 @@
package jscl; package jscl;
import static midpcalc.Real.NumberFormat.FSE_ENG;
import static midpcalc.Real.NumberFormat.FSE_NONE;
import static midpcalc.Real.NumberFormat.FSE_SCI;
import org.solovyev.common.NumberFormatter;
import org.solovyev.common.math.MathRegistry;
import org.solovyev.common.msg.MessageRegistry;
import org.solovyev.common.msg.Messages;
import java.math.BigInteger;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import jscl.math.Expression; import jscl.math.Expression;
import jscl.math.Generic; import jscl.math.Generic;
import jscl.math.NotIntegerException;
import jscl.math.function.Constants; import jscl.math.function.Constants;
import jscl.math.function.ConstantsRegistry; import jscl.math.function.ConstantsRegistry;
import jscl.math.function.Function; import jscl.math.function.Function;
import jscl.math.function.FunctionsRegistry; import jscl.math.function.FunctionsRegistry;
import jscl.math.function.IConstant; import jscl.math.function.IConstant;
import jscl.math.function.PostfixFunctionsRegistry; import jscl.math.function.PostfixFunctionsRegistry;
import jscl.math.function.*;
import jscl.math.operator.Operator; import jscl.math.operator.Operator;
import jscl.math.operator.Percent; import jscl.math.operator.Percent;
import jscl.math.operator.Rand; import jscl.math.operator.Rand;
import jscl.math.operator.matrix.OperatorsRegistry; import jscl.math.operator.matrix.OperatorsRegistry;
import jscl.text.ParseException; import jscl.text.ParseException;
import org.solovyev.common.NumberFormatter;
import org.solovyev.common.math.MathRegistry;
import org.solovyev.common.msg.MessageRegistry;
import org.solovyev.common.msg.Messages;
import static midpcalc.Real.NumberFormat.FSE_ENG;
import static midpcalc.Real.NumberFormat.FSE_NONE;
import static midpcalc.Real.NumberFormat.FSE_SCI;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.math.BigInteger;
import java.util.List;
import static midpcalc.Real.NumberFormat.*;
public class JsclMathEngine implements MathEngine { public class JsclMathEngine implements MathEngine {
@ -38,8 +37,6 @@ public class JsclMathEngine implements MathEngine {
@Nonnull @Nonnull
private static JsclMathEngine instance = new JsclMathEngine(); private static JsclMathEngine instance = new JsclMathEngine();
@Nonnull @Nonnull
private final ConstantsRegistry constantsRegistry = new ConstantsRegistry();
@Nonnull
private final ThreadLocal<NumberFormatter> numberFormatter = new ThreadLocal<NumberFormatter>() { private final ThreadLocal<NumberFormatter> numberFormatter = new ThreadLocal<NumberFormatter>() {
@Override @Override
protected NumberFormatter initialValue() { protected NumberFormatter initialValue() {
@ -104,17 +101,17 @@ public class JsclMathEngine implements MathEngine {
@Nonnull @Nonnull
public MathRegistry<Function> getFunctionsRegistry() { public MathRegistry<Function> getFunctionsRegistry() {
return FunctionsRegistry.getInstance(); return FunctionsRegistry.lazyInstance();
} }
@Nonnull @Nonnull
public MathRegistry<Operator> getOperatorsRegistry() { public MathRegistry<Operator> getOperatorsRegistry() {
return OperatorsRegistry.getInstance(); return OperatorsRegistry.lazyInstance();
} }
@Nonnull @Nonnull
public MathRegistry<Operator> getPostfixFunctionsRegistry() { public MathRegistry<Operator> getPostfixFunctionsRegistry() {
return PostfixFunctionsRegistry.getInstance(); return PostfixFunctionsRegistry.lazyInstance();
} }
@Nonnull @Nonnull
@ -137,7 +134,7 @@ public class JsclMathEngine implements MathEngine {
@Nonnull @Nonnull
public MathRegistry<IConstant> getConstantsRegistry() { public MathRegistry<IConstant> getConstantsRegistry() {
return constantsRegistry; return ConstantsRegistry.lazyInstance();
} }
@Nonnull @Nonnull
@ -202,11 +199,12 @@ public class JsclMathEngine implements MathEngine {
@Nullable @Nullable
private IConstant findConstant(double value) { private IConstant findConstant(double value) {
final IConstant constant = findConstant(constantsRegistry.getSystemEntities(), value); final MathRegistry<IConstant> constants = ConstantsRegistry.getInstance();
final IConstant constant = findConstant(constants.getSystemEntities(), value);
if (constant != null) { if (constant != null) {
return constant; return constant;
} }
final IConstant piInv = constantsRegistry.get(Constants.PI_INV.getName()); final IConstant piInv = constants.get(Constants.PI_INV.getName());
if (piInv != null) { if (piInv != null) {
final Double piInvValue = piInv.getDoubleValue(); final Double piInvValue = piInv.getDoubleValue();
if (piInvValue != null && piInvValue == value) { if (piInvValue != null && piInvValue == value) {

View File

@ -1,17 +1,22 @@
package jscl.math; package jscl.math;
import jscl.JsclMathEngine;
import jscl.math.function.Constant;
import jscl.math.function.Constants;
import jscl.math.function.IConstant;
import jscl.math.numeric.*;
import jscl.mathml.MathML;
import javax.annotation.Nonnull;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
import javax.annotation.Nonnull;
import jscl.math.function.Constant;
import jscl.math.function.Constants;
import jscl.math.function.ConstantsRegistry;
import jscl.math.function.IConstant;
import jscl.math.numeric.Complex;
import jscl.math.numeric.INumeric;
import jscl.math.numeric.Numeric;
import jscl.math.numeric.Real;
import jscl.math.numeric.Vector;
import jscl.mathml.MathML;
public final class NumericWrapper extends Generic implements INumeric<NumericWrapper> { public final class NumericWrapper extends Generic implements INumeric<NumericWrapper> {
@Nonnull @Nonnull
@ -48,7 +53,7 @@ public final class NumericWrapper extends Generic implements INumeric<NumericWra
} }
public NumericWrapper(@Nonnull Constant constant) { public NumericWrapper(@Nonnull Constant constant) {
final IConstant constantFromRegistry = JsclMathEngine.getInstance().getConstantsRegistry().get(constant.getName()); final IConstant constantFromRegistry = ConstantsRegistry.getInstance().get(constant.getName());
if (constantFromRegistry != null) { if (constantFromRegistry != null) {
if (constantFromRegistry.getName().equals(Constants.I.getName())) { if (constantFromRegistry.getName().equals(Constants.I.getName())) {

View File

@ -1,15 +1,19 @@
package jscl.math.function; package jscl.math.function;
import jscl.JsclMathEngine;
import jscl.math.*;
import jscl.mathml.MathML;
import jscl.util.ArrayComparator;
import javax.annotation.Nonnull;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.annotation.Nonnull;
import jscl.math.Generic;
import jscl.math.JsclInteger;
import jscl.math.NotIntegrableException;
import jscl.math.NumericWrapper;
import jscl.math.Variable;
import jscl.mathml.MathML;
import jscl.util.ArrayComparator;
public class Constant extends Variable { public class Constant extends Variable {
public static final int PRIME_CHARS = 3; public static final int PRIME_CHARS = 3;
@ -182,7 +186,7 @@ public class Constant extends Variable {
} }
public String toJava() { public String toJava() {
final IConstant constantFromRegistry = JsclMathEngine.getInstance().getConstantsRegistry().get(getName()); final IConstant constantFromRegistry = ConstantsRegistry.getInstance().get(getName());
if (constantFromRegistry != null) { if (constantFromRegistry != null) {
return constantFromRegistry.toJava(); return constantFromRegistry.toJava();

View File

@ -1,8 +1,10 @@
package jscl.math.function; package jscl.math.function;
import org.solovyev.common.math.AbstractMathRegistry; import org.solovyev.common.math.AbstractMathRegistry;
import org.solovyev.common.math.MathRegistry;
public class ConstantsRegistry extends AbstractMathRegistry<IConstant> { public class ConstantsRegistry extends AbstractMathRegistry<IConstant> {
private static final ConstantsRegistry INSTANCE = new ConstantsRegistry();
public static final String E = "e"; public static final String E = "e";
public static final String C = "c"; public static final String C = "c";
@ -14,15 +16,28 @@ public class ConstantsRegistry extends AbstractMathRegistry<IConstant> {
public final static String NAN = "NaN"; public final static String NAN = "NaN";
public ConstantsRegistry() { public ConstantsRegistry() {
this.add(new PiConstant()); }
this.add(new ExtendedConstant(Constants.PI_INV, Math.PI, null));
this.add(new ExtendedConstant(Constants.INF, Double.POSITIVE_INFINITY, "JsclDouble.valueOf(Double.POSITIVE_INFINITY)")); @Override
this.add(new ExtendedConstant(Constants.INF_2, Double.POSITIVE_INFINITY, "JsclDouble.valueOf(Double.POSITIVE_INFINITY)")); public void onInit() {
this.add(new ExtendedConstant(Constants.I, "√(-1)", null)); add(new PiConstant());
this.add(new ExtendedConstant(new Constant(E), Math.E, null)); add(new ExtendedConstant(Constants.PI_INV, Math.PI, null));
this.add(new ExtendedConstant(new Constant(C), C_VALUE, null)); add(new ExtendedConstant(Constants.INF, Double.POSITIVE_INFINITY, "JsclDouble.valueOf(Double.POSITIVE_INFINITY)"));
this.add(new ExtendedConstant(new Constant(G), G_VALUE, null)); add(new ExtendedConstant(Constants.INF_2, Double.POSITIVE_INFINITY, "JsclDouble.valueOf(Double.POSITIVE_INFINITY)"));
this.add(new ExtendedConstant(new Constant(H_REDUCED), H_REDUCED_VALUE, null)); add(new ExtendedConstant(Constants.I, "√(-1)", null));
this.add(new ExtendedConstant(new Constant(NAN), Double.NaN, null)); add(new ExtendedConstant(new Constant(E), Math.E, null));
add(new ExtendedConstant(new Constant(C), C_VALUE, null));
add(new ExtendedConstant(new Constant(G), G_VALUE, null));
add(new ExtendedConstant(new Constant(H_REDUCED), H_REDUCED_VALUE, null));
add(new ExtendedConstant(new Constant(NAN), Double.NaN, null));
}
public static MathRegistry<IConstant> getInstance() {
INSTANCE.init();
return INSTANCE;
}
public static MathRegistry<IConstant> lazyInstance() {
return INSTANCE;
} }
} }

View File

@ -1,12 +1,27 @@
package jscl.math.function; package jscl.math.function;
import jscl.math.Variable;
import jscl.math.function.hyperbolic.*;
import jscl.math.function.trigonometric.*;
import org.solovyev.common.math.AbstractMathRegistry; import org.solovyev.common.math.AbstractMathRegistry;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import jscl.math.Variable;
import jscl.math.function.hyperbolic.Acosh;
import jscl.math.function.hyperbolic.Acoth;
import jscl.math.function.hyperbolic.Asinh;
import jscl.math.function.hyperbolic.Atanh;
import jscl.math.function.hyperbolic.Cosh;
import jscl.math.function.hyperbolic.Coth;
import jscl.math.function.hyperbolic.Sinh;
import jscl.math.function.hyperbolic.Tanh;
import jscl.math.function.trigonometric.Acos;
import jscl.math.function.trigonometric.Acot;
import jscl.math.function.trigonometric.Asin;
import jscl.math.function.trigonometric.Atan;
import jscl.math.function.trigonometric.Cos;
import jscl.math.function.trigonometric.Cot;
import jscl.math.function.trigonometric.Sin;
import jscl.math.function.trigonometric.Tan;
/** /**
* User: serso * User: serso
* Date: 10/29/11 * Date: 10/29/11
@ -16,50 +31,14 @@ public class FunctionsRegistry extends AbstractMathRegistry<Function> {
private final static FunctionsRegistry instance = new FunctionsRegistry(); private final static FunctionsRegistry instance = new FunctionsRegistry();
static {
instance.add(new Deg(null));
instance.add(new Rad(null, null, null));
instance.add(new Dms(null, null, null));
instance.add(new Sin(null));
instance.add(new Cos(null));
instance.add(new Tan(null));
instance.add(new Cot(null));
instance.add(new Asin(null));
instance.add(new Acos(null));
instance.add(new Atan(null));
instance.add(new Acot(null));
instance.add(new Ln(null));
instance.add(new Lg(null));
instance.add(new Exp(null));
instance.add(new Sqrt(null));
instance.add(new Cubic(null));
instance.add(new Sinh(null));
instance.add(new Cosh(null));
instance.add(new Tanh(null));
instance.add(new Coth(null));
instance.add(new Asinh(null));
instance.add(new Acosh(null));
instance.add(new Atanh(null));
instance.add(new Acoth(null));
instance.add(new Abs(null));
instance.add(new Sgn(null));
instance.add(new Conjugate(null));
for (String name : Comparison.names) {
instance.add(new Comparison(name, null, null));
}
}
@Nonnull @Nonnull
public static FunctionsRegistry getInstance() { public static FunctionsRegistry getInstance() {
instance.init();
return instance;
}
@Nonnull
public static FunctionsRegistry lazyInstance() {
return instance; return instance;
} }
@ -78,4 +57,46 @@ public class FunctionsRegistry extends AbstractMathRegistry<Function> {
final Function function = super.get(name); final Function function = super.get(name);
return function == null ? null : copy(function); return function == null ? null : copy(function);
} }
@Override
public void onInit() {
add(new Deg(null));
add(new Rad(null, null, null));
add(new Dms(null, null, null));
add(new Sin(null));
add(new Cos(null));
add(new Tan(null));
add(new Cot(null));
add(new Asin(null));
add(new Acos(null));
add(new Atan(null));
add(new Acot(null));
add(new Ln(null));
add(new Lg(null));
add(new Exp(null));
add(new Sqrt(null));
add(new Cubic(null));
add(new Sinh(null));
add(new Cosh(null));
add(new Tanh(null));
add(new Coth(null));
add(new Asinh(null));
add(new Acosh(null));
add(new Atanh(null));
add(new Acoth(null));
add(new Abs(null));
add(new Sgn(null));
add(new Conjugate(null));
for (String name : Comparison.names) {
add(new Comparison(name, null, null));
}
}
} }

View File

@ -1,12 +1,17 @@
package jscl.math.function; package jscl.math.function;
import jscl.math.Generic;
import jscl.math.operator.*;
import org.solovyev.common.math.AbstractMathRegistry; import org.solovyev.common.math.AbstractMathRegistry;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import jscl.math.Generic;
import jscl.math.operator.Degree;
import jscl.math.operator.DoubleFactorial;
import jscl.math.operator.Factorial;
import jscl.math.operator.Operator;
import jscl.math.operator.Percent;
/** /**
* User: serso * User: serso
* Date: 10/31/11 * Date: 10/31/11
@ -16,15 +21,14 @@ public class PostfixFunctionsRegistry extends AbstractMathRegistry<Operator> {
private final static PostfixFunctionsRegistry instance = new PostfixFunctionsRegistry(); private final static PostfixFunctionsRegistry instance = new PostfixFunctionsRegistry();
static { @Nonnull
instance.add(new DoubleFactorial(null)); public static PostfixFunctionsRegistry getInstance() {
instance.add(new Factorial(null)); instance.init();
instance.add(new Degree(null)); return instance;
instance.add(new Percent(null, null));
} }
@Nonnull @Nonnull
public static PostfixFunctionsRegistry getInstance() { public static PostfixFunctionsRegistry lazyInstance() {
return instance; return instance;
} }
@ -40,4 +44,11 @@ public class PostfixFunctionsRegistry extends AbstractMathRegistry<Operator> {
return operator == null ? null : FunctionsRegistry.copy(operator); return operator == null ? null : FunctionsRegistry.copy(operator);
} }
@Override
public void onInit() {
add(new DoubleFactorial(null));
add(new Factorial(null));
add(new Degree(null));
add(new Percent(null, null));
}
} }

View File

@ -1,13 +1,20 @@
package jscl.math.operator.matrix; package jscl.math.operator.matrix;
import jscl.math.Generic;
import jscl.math.function.FunctionsRegistry;
import jscl.math.operator.*;
import org.solovyev.common.math.AbstractMathRegistry; import org.solovyev.common.math.AbstractMathRegistry;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import jscl.math.Generic;
import jscl.math.function.FunctionsRegistry;
import jscl.math.operator.Derivative;
import jscl.math.operator.IndefiniteIntegral;
import jscl.math.operator.Integral;
import jscl.math.operator.Modulo;
import jscl.math.operator.Operator;
import jscl.math.operator.Product;
import jscl.math.operator.Sum;
/** /**
* User: serso * User: serso
* Date: 11/17/11 * Date: 11/17/11
@ -17,91 +24,14 @@ public class OperatorsRegistry extends AbstractMathRegistry<Operator> {
private final static OperatorsRegistry instance = new OperatorsRegistry(); private final static OperatorsRegistry instance = new OperatorsRegistry();
static {
instance.add(new Derivative(null, null, null, null));
//instance.add(new Grad(null, null));
//instance.add(new Divergence(null, null));
//instance.add(new Curl(null, null));
//instance.add(new Jacobian(null, null));
//instance.add(new Laplacian(null, null));
//instance.add(new Dalembertian(null, null));
//instance.add(new Del(null, null, null));
//instance.add(new VectorProduct(null, null));
//instance.add(new ComplexProduct(null, null));
//instance.add(new QuaternionProduct(null, null));
//instance.add(new GeometricProduct(null, null, null));
//instance.add(new MatrixProduct(null, null));
//instance.add(new TensorProduct(null, null));
//instance.add(new Transpose(null));
//instance.add(new Trace(null));
//instance.add(new Determinant(null));
//instance.add(new Coefficient(null, null));
//instance.add(new Solve(null, null, null));
//instance.add(new Substitute(null, null, null));
//instance.add(new Limit(null, null, null, null));
instance.add(new Sum(null, null, null, null));
instance.add(new Product(null, null, null, null));
//instance.add(new Groebner(null, null, null, null));
//instance.add(new Division(null, null));
instance.add(new Modulo(null, null));
//instance.add(new ModPow(null, null, null));
//instance.add(new ModInverse(null, null));
//instance.add(new EulerPhi(null));
instance.add(new Integral(null, null, null, null));
instance.add(new IndefiniteIntegral(null, null));
//instance.add(new Rand());
//instance.add(new Mean(null));
//instance.add(new Min(null));
//instance.add(new Max(null));
//instance.add(new MeanSquareDeviation(null));
//instance.add(new StandardDeviation(null));
//instance.add(new PrimitiveRoots(null));
}
/* if (operatorName.compareTo("d") == 0)
v = new Derivative(parameters[0], parameters[1], parameters.length > 2 ? parameters[2] : parameters[1], parameters.length > 3 ? parameters[3] : JsclInteger.valueOf(1));
else if (operatorName.compareTo("grad") == 0) v = new Grad(parameters[0], parameters[1]);
else if (operatorName.compareTo("diverg") == 0) v = new Divergence(parameters[0], parameters[1]);
else if (operatorName.compareTo("curl") == 0) v = new Curl(parameters[0], parameters[1]);
else if (operatorName.compareTo("jacobian") == 0) v = new Jacobian(parameters[0], parameters[1]);
else if (operatorName.compareTo("laplacian") == 0) v = new Laplacian(parameters[0], parameters[1]);
else if (operatorName.compareTo("dalembertian") == 0) v = new Dalembertian(parameters[0], parameters[1]);
else if (operatorName.compareTo("del") == 0)
v = new Del(parameters[0], parameters[1], parameters.length > 2 ? parameters[2] : JsclInteger.valueOf(0));
else if (operatorName.compareTo("vector") == 0) v = new VectorProduct(parameters[0], parameters[1]);
else if (operatorName.compareTo("complex") == 0) v = new ComplexProduct(parameters[0], parameters[1]);
else if (operatorName.compareTo("quaternion") == 0) v = new QuaternionProduct(parameters[0], parameters[1]);
else if (operatorName.compareTo("geometric") == 0)
v = new GeometricProduct(parameters[0], parameters[1], parameters.length > 2 ? parameters[2] : JsclInteger.valueOf(0));
else if (operatorName.compareTo("matrix") == 0) v = new MatrixProduct(parameters[0], parameters[1]);
else if (operatorName.compareTo("tensor") == 0) v = new TensorProduct(parameters[0], parameters[1]);
else if (operatorName.compareTo("tran") == 0) v = new Transpose(parameters[0]);
else if (operatorName.compareTo("trace") == 0) v = new Trace(parameters[0]);
else if (operatorName.compareTo("det") == 0) v = new Determinant(parameters[0]);
else if (operatorName.compareTo("coef") == 0) v = new Coefficient(parameters[0], parameters[1]);
else if (operatorName.compareTo("solve") == 0)
v = new Solve(parameters[0], parameters[1], parameters.length > 2 ? parameters[2] : JsclInteger.valueOf(0));
else if (operatorName.compareTo("subst") == 0)
v = new Substitute(parameters[0], parameters[1], parameters[2]).transmute();
else if (operatorName.compareTo("lim") == 0)
v = new Limit(parameters[0], parameters[1], parameters[2], parameters.length > 3 && (parameters[2].compareTo(Constant.infinity) != 0 && parameters[2].compareTo(Constant.infinity.negate()) != 0) ? JsclInteger.valueOf(parameters[3].signum()) : JsclInteger.valueOf(0));
else if (operatorName.compareTo("sum") == 0)
v = new Sum(parameters[0], parameters[1], parameters[2], parameters[3]);
else if (operatorName.compareTo("prod") == 0)
v = new Product(parameters[0], parameters[1], parameters[2], parameters[3]);
else if (operatorName.compareTo("integral") == 0)
v = parameters.length > 2 ? new Integral(parameters[0], parameters[1], parameters[2], parameters[3]) : new IndefiniteIntegral(parameters[0], parameters[1]);
else if (operatorName.compareTo("groebner") == 0)
v = new Groebner(parameters[0], parameters[1], parameters.length > 2 ? parameters[2] : Expression.valueOf("lex"), parameters.length > 3 ? parameters[3] : JsclInteger.valueOf(0)).transmute();
else if (operatorName.compareTo("div") == 0) v = new Division(parameters[0], parameters[1]);
else if (operatorName.compareTo("mod") == 0) v = new Modulo(parameters[0], parameters[1]);
else if (operatorName.compareTo("modpow") == 0) v = new ModPow(parameters[0], parameters[1], parameters[2]);
else if (operatorName.compareTo("modinv") == 0) v = new ModInverse(parameters[0], parameters[1]);
else if (operatorName.compareTo("eulerphi") == 0) v = new EulerPhi(parameters[0]);
else if (operatorName.compareTo("primitiveroots") == 0) v = new PrimitiveRoots(parameters[0]);*/
@Nonnull @Nonnull
public static OperatorsRegistry getInstance() { public static OperatorsRegistry getInstance() {
instance.init();
return instance;
}
@Nonnull
public static OperatorsRegistry lazyInstance() {
return instance; return instance;
} }
@ -124,4 +54,14 @@ public class OperatorsRegistry extends AbstractMathRegistry<Operator> {
final Operator operator = super.get(name); final Operator operator = super.get(name);
return operator == null ? null : FunctionsRegistry.copy(operator); return operator == null ? null : FunctionsRegistry.copy(operator);
} }
@Override
public void onInit() {
add(new Derivative(null, null, null, null));
add(new Sum(null, null, null, null));
add(new Product(null, null, null, null));
add(new Modulo(null, null));
add(new Integral(null, null, null, null));
add(new IndefiniteIntegral(null, null));
}
} }

View File

@ -52,10 +52,27 @@ public abstract class AbstractMathRegistry<T extends MathEntity> implements Math
@GuardedBy("this") @GuardedBy("this")
@Nonnull @Nonnull
protected final SortedList<T> systemEntities = SortedList.newInstance(new ArrayList<T>(30), MATH_ENTITY_COMPARATOR); protected final SortedList<T> systemEntities = SortedList.newInstance(new ArrayList<T>(30), MATH_ENTITY_COMPARATOR);
private volatile boolean initialized;
protected AbstractMathRegistry() { protected AbstractMathRegistry() {
} }
@Override
public final void init() {
if (initialized) {
return;
}
synchronized (this) {
if (initialized) {
return;
}
onInit();
initialized = true;
}
}
protected abstract void onInit();
@Nonnull @Nonnull
private static synchronized Integer count() { private static synchronized Integer count() {
final Integer result = counter; final Integer result = counter;

View File

@ -22,9 +22,10 @@
package org.solovyev.common.math; package org.solovyev.common.math;
import java.util.List;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.List;
public interface MathRegistry<T extends MathEntity> { public interface MathRegistry<T extends MathEntity> {
@ -48,4 +49,6 @@ public interface MathRegistry<T extends MathEntity> {
@Nullable @Nullable
T getById(@Nonnull Integer id); T getById(@Nonnull Integer id);
void init();
} }