Implement EquationsSystemPattern

This commit is contained in:
Riccardo Azzolini 2018-10-16 19:36:34 +02:00
parent c2dc02c0e1
commit b65723a2c6
2 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,50 @@
package it.cavallium.warppi.math.rules.dsl.patterns;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.functions.equations.EquationsSystem;
import it.cavallium.warppi.math.rules.dsl.Pattern;
import it.cavallium.warppi.math.rules.dsl.PatternUtils;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
/**
* Matches and generates a system of equations of multiple other patterns.
*/
public class EquationsSystemPattern extends VisitorPattern {
final Pattern[] patterns;
public EquationsSystemPattern(final Pattern[] patterns) {
this.patterns = patterns;
}
@Override
public Optional<Map<String, Function>> visit(final EquationsSystem equationsSystem) {
if (patterns.length != equationsSystem.getParametersLength()) {
return Optional.empty();
}
Optional<Map<String, Function>> subFunctions = Optional.of(new HashMap<>());
for (int i = 0; i < patterns.length && subFunctions.isPresent(); i++) {
final Pattern curPattern = patterns[i];
final Function curFunction = equationsSystem.getParameter(i);
subFunctions = subFunctions
.flatMap(prevMatch -> curPattern.match(curFunction)
.flatMap(curMatch -> PatternUtils.mergeMatches(prevMatch, curMatch))
);
}
return subFunctions;
}
@Override
public Function replace(final MathContext mathContext, final Map<String, Function> subFunctions) {
final Function[] functions = Arrays.stream(patterns)
.map(pattern -> pattern.replace(mathContext, subFunctions))
.toArray(Function[]::new);
return new EquationsSystem(mathContext, functions);
}
}

View File

@ -5,6 +5,7 @@ import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.functions.*;
import it.cavallium.warppi.math.functions.Number;
import it.cavallium.warppi.math.functions.equations.Equation;
import it.cavallium.warppi.math.functions.equations.EquationsSystem;
import it.cavallium.warppi.math.functions.trigonometry.*;
import it.cavallium.warppi.math.rules.dsl.patterns.*;
import org.apache.commons.lang3.tuple.ImmutablePair;
@ -129,6 +130,36 @@ public class PatternTest {
assertTrue(pattern.replace(mathContext, subFunctions.get()) instanceof Undefined);
}
@Test
public void equationsSystemPattern() {
final Pattern pattern = new EquationsSystemPattern(new Pattern[]{
new SubFunctionPattern("x"),
new SubFunctionPattern("y"),
new SubFunctionPattern("z")
});
final Function shouldNotMatch = new EquationsSystem(
mathContext,
new Function[]{
new Number(mathContext, 1),
new Number(mathContext, 2),
}
);
assertFalse(pattern.match(shouldNotMatch).isPresent());
final Function shouldMatch = new EquationsSystem(
mathContext,
new Function[]{
new Number(mathContext, 1),
new Number(mathContext, 2),
new Number(mathContext, 3)
}
);
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent());
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
}
@Test
public void otherBinaryPatterns() {
final Number one = new Number(mathContext, 1);