Skip to content

Commit 967188b

Browse files
committed
Added || shorthand for abs and snorm
1 parent ab518d3 commit 967188b

File tree

11 files changed

+696
-592
lines changed

11 files changed

+696
-592
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ You can also get the node from comfy manager under the name of More math.
1818
- Nodes for CONDITIONING, LATENT, IMAGE, NOISE, FLOAT, VIDEO and AUDIO
1919

2020
## Operators
21-
- Math: `+`, `-`, `*`, `/`, `%`, `^`
21+
- Math: `+`, `-`, `*`, `/`, `%`, `^`, `||`
2222
- Boolean: `<`, `<=`, `>`, `>=`, `==`, `!=`
2323
(`false = 0.0`, `true = 1.0`)
2424

2525
## Functions
2626

2727
### Basic Math
28-
- `abs(x)`: Absolute value.
28+
- `abs(x)` or `|x|`: Absolute value. For **float** `abs(x)` and `|x|` are the same. For **tensor** `abs(x)` calculates element-wise absolute value and for `|x|` it calculates L2 norm (euclidean norm).
2929
- `sqrt(x)`: Square root.
3030
- `ln(x)`: Natural logarithm (base e).
3131
- `log(x)`: Logarithm base 10.

more_math/Parser/FloatEvalVisitor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def visitAsinhFunc(self, ctx): return math.asinh(self.visit(ctx.expr()))
105105
def visitAcoshFunc(self, ctx): return math.acosh(self.visit(ctx.expr()))
106106
def visitAtanhFunc(self, ctx): return math.atanh(self.visit(ctx.expr()))
107107
def visitAbsFunc(self, ctx): return math.abs(self.visit(ctx.expr()))
108+
def visitAbsExp(self, ctx): return math.abs(self.visit(ctx.expr()))
108109
def visitSqrtFunc(self, ctx): return math.sqrt(self.visit(ctx.expr()))
109110
def visitLnFunc(self, ctx): return math.log(self.visit(ctx.expr()))
110111
def visitLogFunc(self, ctx): return math.log10(self.visit(ctx.expr()))

more_math/Parser/MathExpr.g4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ atom
5050
| NUMBER # NumberExp
5151
| CONSTANT # ConstantExp
5252
| '(' expr ')' # ParenExp
53+
| PIPE expr PIPE # AbsExp
5354
;
5455

5556
// Single-argument functions
@@ -174,6 +175,7 @@ LE : '<=';
174175
LT : '<';
175176
EQ : '==';
176177
NE : '!=';
178+
PIPE : '|';
177179

178180
CONSTANT : ('pi'|'PI'|'e'|'E');
179181
NUMBER : [0-9]+ ('.' [0-9]+)?;

more_math/Parser/MathExpr.interp

Lines changed: 22 additions & 1 deletion
Large diffs are not rendered by default.

more_math/Parser/MathExpr.tokens

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ LE=56
5757
LT=57
5858
EQ=58
5959
NE=59
60-
CONSTANT=60
61-
NUMBER=61
62-
VARIABLE=62
63-
WS=63
60+
PIPE=60
61+
CONSTANT=61
62+
NUMBER=62
63+
VARIABLE=63
64+
WS=64
6465
'('=1
6566
')'=2
6667
','=3
@@ -120,3 +121,4 @@ WS=63
120121
'<'=57
121122
'=='=58
122123
'!='=59
124+
'|'=60

more_math/Parser/MathExprLexer.interp

Lines changed: 31 additions & 1 deletion
Large diffs are not rendered by default.

more_math/Parser/MathExprLexer.py

Lines changed: 178 additions & 174 deletions
Large diffs are not rendered by default.

more_math/Parser/MathExprLexer.tokens

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ LE=56
5757
LT=57
5858
EQ=58
5959
NE=59
60-
CONSTANT=60
61-
NUMBER=61
62-
VARIABLE=62
63-
WS=63
60+
PIPE=60
61+
CONSTANT=61
62+
NUMBER=62
63+
VARIABLE=63
64+
WS=64
6465
'('=1
6566
')'=2
6667
','=3
@@ -120,3 +121,4 @@ WS=63
120121
'<'=57
121122
'=='=58
122123
'!='=59
124+
'|'=60

more_math/Parser/MathExprParser.py

Lines changed: 440 additions & 404 deletions
Large diffs are not rendered by default.

more_math/Parser/MathExprVisitor.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Generated from src/more_math/Parser/MathExpr.g4 by ANTLR 4.13.2
2-
from antlr4 import ParseTreeVisitor
1+
# Generated from ./MathExpr.g4 by ANTLR 4.13.2
2+
from antlr4 import *
33
if "." in __name__:
44
from .MathExprParser import MathExprParser
55
else:
@@ -154,6 +154,11 @@ def visitParenExp(self, ctx:MathExprParser.ParenExpContext):
154154
return self.visitChildren(ctx)
155155

156156

157+
# Visit a parse tree produced by MathExprParser#AbsExp.
158+
def visitAbsExp(self, ctx:MathExprParser.AbsExpContext):
159+
return self.visitChildren(ctx)
160+
161+
157162
# Visit a parse tree produced by MathExprParser#SinFunc.
158163
def visitSinFunc(self, ctx:MathExprParser.SinFuncContext):
159164
return self.visitChildren(ctx)

0 commit comments

Comments
 (0)