Comments (7)
Yes, you can create your own subclass of the Constraint
class. You can have a look at the documentation to see which methods you need to implement to do that. Fo example:
class FreeQ(Constraint):
def __init__(self, vars, x):
self.vars = frozenset(v.name for v in vars)
self.x = x
def __call__(self. substitution):
for v in self.vars:
if substitution[v].contains(x):
return False
return True
@property
def variables(self):
return self.vars
def with_renamed_vars(self.renaming):
copy = FreeQ([], self.x)
copy.vars = frozenset(renaming.get(v, v) for v in self.vars)
return copy
def __eq__(self, other):
return isinstance(other, FreeQ) and other.vars == self.vars and other.x == self.x
def __hash__(self):
return hash((self.vars, self.x))
The variables
property is used to the many-to-one matcher can optimize when the test the constraint (as soon as all variables have a substitution). This enables earlier backtracking and should speedup the matching.
The with_renamed_vars
method creates a copy with renamed variables. This is used to make sure that parts of patterns that are the same except for the variable names can be matched together by the many-to-one matcher. I plan on solving this differently and getting rid of the method in the future.
from matchpy.
I have implemented class NonzeroQ()
:
class NonzeroQ(Constraint):
def __init__(self, expr, vars):
self.expr = expr
self.vars = frozenset(v.name for v in vars)
def __call__(self, substitution):
subst = {}
for i in self.vars:
subst[i] = substitution[i]
if substitute(self.expr, subst).symbols == {'+'}:
return False
return True
@property
def variables(self):
return self.vars
def with_renamed_vars(self, renaming):
copy = NonzeroQ()
copy.vars = frozenset(renaming.get(v, v) for v in self.vars)
return copy
def __eq__(self, other):
return isinstance(other, NonzeroQ) and other.vars == self.vars and other.expr == self.expr
def __hash__(self):
return hash(self.vars)
pattern = Pattern(Int(Pow(x_, m_), x), FreeQ((m,), x), NonzeroQ(Add(m_, one), (m,)))
Can you suggest a better implementation of __call__
function?
from matchpy.
First of all, you don't need to copy the substitution before you substitute.
Secondly, assuming you have implemented some kind of simplications that turn something like Add(1, -1) into 0, you can use all(substitution[v] != 0 for v in self.vars)
.
Or you use CustomConstraint(lambda m: Add(m, one) != 0)
.
I don't know how NonzeroQ in Rubi is implemented though.
from matchpy.
In NonzeroQ
, I am trying to substitute substitution
variable in sef.expr
.
This works fine:
def __call__(self, substitution):
if substitute(self.expr, substitution).symbols == ():
return False
return True
from matchpy.
Mathematica implementation on NonzeroQ()
:
(* ZeroQ[u1,u2,...] returns True if u1, u2, ... are all 0; else it returns False. *)
ZeroQ[u_] := Quiet[PossibleZeroQ[u]]
ZeroQ[u__] := Catch[Scan[Function[If[ZeroQ[#],Null,Throw[False]]],{u}];True]
NonzeroQ[u_] := Not[ZeroQ[u]]
from matchpy.
Looks like you need your own implementation of PossibleZeroQ to properly check if something is zero. Or you need a advanced simplification algorithm so that you have Add(Div(Mul(a, -1), a), 1) == 0
.
from matchpy.
For Rubi, @Upabjojr already adviced to have sympy
to matchpy
(and vice versa) converters. Perhaps we can convert matchpy
expression into sympy
and then do mathematical operations.
from matchpy.
Related Issues (20)
- Need Help in code generation. HOT 7
- Not Compatible with Python 3.7 HOT 4
- Wildcard that matches multiple symbols HOT 1
- Create `new` class method on Symbol HOT 1
- ManyToOneMatcher: Position of constraints logic HOT 3
- GPL'd dependency: hopcroftkarp library HOT 6
- Usage of type unions HOT 1
- [Feature] Visualization of Expressions with Graphviz
- ANN: MatchPyCpp port to C++ HOT 7
- New release? HOT 5
- Remove dependency on pkg_resources/setuptools HOT 1
- Inconsistent output of match_anywhere HOT 4
- Improve checking correctness of input
- Match arbitrary function HOT 4
- One-to-one matching: symbols with variable_name do not work in commutative functions HOT 2
- substitute not working with SymPy HOT 11
- Optional wildcards to match the identity element of the current node HOT 7
- FreeQ equivalent in MatchPy? HOT 1
- New MatchPy version? HOT 5
- Expression matching with patterns without wildcards HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from matchpy.