Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: summation #288

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
37 changes: 14 additions & 23 deletions public/dimensional_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
ceiling,
sign,
sqrt,
factorial
factorial,
summation
)

class ExprWithAssumptions(Expr):
Expand Down Expand Up @@ -1124,32 +1125,19 @@ def UniversalInverse(expression: Expr) -> Expr:

def IndexMatrix(expression: Expr, i: Expr, j: Expr) -> Expr:
for subscript in cast(list[ExprWithAssumptions], (i,j)):
if not (subscript.is_real and subscript.is_finite and subscript.is_integer and cast(int, subscript) > 0):
raise Exception("Matrix indices must evaluate to a finite real integer and be greater than 0")
if subscript.is_real and cast(int, subscript) <= 0:
raise Exception("Matrix indices must be greater than 0")

return expression[i-1, j-1] # type: ignore

class CustomFactorial(Function):
is_real = True

@staticmethod
def _imp_(arg1: float):
if arg1.is_integer() and arg1 >= 0.0:
return math.factorial(int(arg1))
else:
raise ValueError("The factorial function can only be evaluated on a nonnegative integer")

def _eval_evalf(self, prec):
if self.args[0].is_number:
if not (self.args[0].is_real and
cast(ExprWithAssumptions, self.args[0]).is_finite and
cast(ExprWithAssumptions, self.args[0]).is_integer and
cast(int, self.args[0]) >= 0):
raise ValueError("The factorial function can only be evaluated on a nonnegative integer")
return factorial(self.args[0])._eval_evalf(prec) # type: ignore
def _factorial_imp_(arg1: float):
if arg1.is_integer() and arg1 >= 0.0:
return math.factorial(int(arg1))
else:
raise ValueError("The factorial function can only be evaluated on a nonnegative integer")

def _latex(self, printer):
return rf"\left({printer._print(self.args[0])}\right)!"
factorial._imp_ = staticmethod(_factorial_imp_) # type: ignore

def custom_norm(expression: Matrix):
return expression.norm()
Expand Down Expand Up @@ -1185,6 +1173,8 @@ def custom_integral_dims(local_expr: Expr, global_expr: Expr, dummy_integral_var
else:
return global_expr * integral_var # type: ignore

def custom_summation(operand: Expr, dummy_var: Symbol, start: Expr, end: Expr):
return summation(operand, (dummy_var, start, end))

CP = None

Expand Down Expand Up @@ -1493,7 +1483,8 @@ def get_next_id(self):
cast(Function, Function('_Derivative')) : {"dim_func": custom_derivative_dims, "sympy_func": custom_derivative},
cast(Function, Function('_Integral')) : {"dim_func": custom_integral_dims, "sympy_func": custom_integral},
cast(Function, Function('_range')) : {"dim_func": custom_range, "sympy_func": custom_range},
cast(Function, Function('_factorial')) : {"dim_func": factorial, "sympy_func": CustomFactorial},
cast(Function, Function('_factorial')) : {"dim_func": factorial, "sympy_func": factorial},
cast(Function, Function('_summation')) : {"dim_func": custom_summation, "sympy_func": custom_summation},
}

global_placeholder_set = set(global_placeholder_map.keys())
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export const INLINE_SHORTCUTS = {
'~': '\\approx',
'sqrt': '\\sqrt{#?}',
'$int': '\\int _{#?}^{#?}\\left(#?\\right)\\mathrm{d}\\left(#?\\right)',
'$inf': '\\infty',
'$sum': '\\sum_{#?=#?}^{#?}\\left(#?\\right)',
'$prime': '\\frac{\\mathrm{d}}{\\mathrm{d}\\left(#?\\right)}\\left(#?\\right)',
'$doubleprime': '\\frac{\\mathrm{d}^{2}}{\\mathrm{d}\\left(#?\\right)^{2}}\\left(#?\\right)',
'$tripleprime': '\\frac{\\mathrm{d}^{3}}{\\mathrm{d}\\left(#?\\right)^{3}}\\left(#?\\right)',
Expand Down
20 changes: 15 additions & 5 deletions src/parser/LatexLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,24 @@ DOUBLE_VBAR: '||' | '\\Vert' ;

UNDERSCORE: '_' ;

fragment
UNDERSCORE_SINGLE_CHAR_NUMBER: '_' [0-9];

fragment
UNDERSCORE_SINGLE_CHAR_ID: '_' [a-zA-Z];

EXCLAMATION: '!' ;

PI: '\\pi' ;

INFINITY: '\\infty' ;

CMD_INT: '\\int' ;
CMD_INT_UNDERSCORE: '\\int' [ ]* '_' ;
CMD_INT_UNDERSCORE_SINGLE_CHAR_NUMBER: '\\int' [ ]* '_' [0-9];
CMD_INT_UNDERSCORE_SINGLE_CHAR_ID: '\\int' [ ]* '_' [a-zA-Z] ;
CMD_INT_UNDERSCORE_SINGLE_CHAR_NUMBER: '\\int' [ ]* UNDERSCORE_SINGLE_CHAR_NUMBER ;
CMD_INT_UNDERSCORE_SINGLE_CHAR_ID: '\\int' [ ]* UNDERSCORE_SINGLE_CHAR_ID ;

CMD_SUM_UNDERSCORE: '\\sum' [ ]* '_' ;

CMD_MATHRM: '\\mathrm' ;

Expand Down Expand Up @@ -62,8 +72,8 @@ CMD_COTH: 'coth' ;
CMD_LN: 'ln' ;
CMD_LOG: 'log' ;
CMD_SLASH_LOG_UNDERSCORE: '\\log' [ ]* '_' ;
CMD_SLASH_LOG_UNDERSCORE_SINGLE_CHAR_NUMBER: '\\log' [ ]* '_' [0-9] ;
CMD_SLASH_LOG_UNDERSCORE_SINGLE_CHAR_ID: '\\log' [ ]* '_' [a-zA-Z] ;
CMD_SLASH_LOG_UNDERSCORE_SINGLE_CHAR_NUMBER: '\\log' [ ]* UNDERSCORE_SINGLE_CHAR_NUMBER ;
CMD_SLASH_LOG_UNDERSCORE_SINGLE_CHAR_ID: '\\log' [ ]* UNDERSCORE_SINGLE_CHAR_ID ;

COMMENT: '\\text{' .*? '}' -> skip ;

Expand Down Expand Up @@ -116,7 +126,7 @@ AMPERSAND: '&';
DOUBLE_BACKSLASH: '\\\\';


UNDERSCORE_SUBSCRIPT: (([ ]* '_{' [a-zA-Z0-9]+ '}') | ([ ]* '_' [a-zA-Z0-9]));
UNDERSCORE_SUBSCRIPT: (([ ]* '_{' [a-zA-Z0-9]+ '}') | ([ ]* (UNDERSCORE_SINGLE_CHAR_ID | UNDERSCORE_SINGLE_CHAR_NUMBER) ));

CARET_SINGLE_CHAR_ID_UNDERSCORE_SUBSCRIPT: '^'[a-zA-Z] UNDERSCORE_SUBSCRIPT;

Expand Down
Loading
Loading