Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions sympy/printing/octave.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,31 @@

# List of known functions. First, those that have the same name in
# SymPy and Octave. This is almost certainly incomplete!
known_fcns_src1 = ["sin", "cos", "tan", "asin", "acos", "atan", "atan2",
"sinh", "cosh", "tanh", "asinh", "acosh", "atanh",
"log", "exp", "erf", "gamma", "sign", "floor", "csc",
"sec", "cot", "coth", "acot", "acoth", "erfc",
"besselj", "bessely", "besseli", "besselk",
"erfinv", "erfcinv", "factorial" ]
known_fcns_src1 = ["sin", "cos", "tan", "cot", "sec", "csc",
"asin", "acos", "acot", "atan", "atan2", "asec", "acsc",
"sinh", "cosh", "tanh", "coth", "csch", "sech",
"asinh", "acosh", "atanh", "acoth", "asech", "acsch",
"erfc", "erfi", "erf", "erfinv", "erfcinv",
"besseli", "besselj", "besselk", "bessely",
"exp", "factorial", "floor", "fresnelc", "fresnels",
"gamma", "log", "polylog", "sign", "zeta"]

# These functions have different names ("Sympy": "Octave"), more
# generally a mapping to (argument_conditions, octave_function).
known_fcns_src2 = {
"Abs": "abs",
"ceiling": "ceil",
"Chi": "coshint",
"Ci": "cosint",
"conjugate": "conj",
"DiracDelta": "dirac",
"Heaviside": "heaviside",
"laguerre": "laguerreL",
"li": "logint",
"loggamma": "gammaln",
"polygamma": "psi",
"Shi": "sinhint",
"Si": "sinint",
}


Expand Down Expand Up @@ -371,6 +382,16 @@ def _print_Identity(self, expr):
return "eye(%s)" % self._print(expr.shape[0])


def _print_uppergamma(self, expr):
return "gammainc(%s, %s, 'upper')" % (self._print(expr.args[1]),
self._print(expr.args[0]))


def _print_lowergamma(self, expr):
return "gammainc(%s, %s, 'lower')" % (self._print(expr.args[1]),
self._print(expr.args[0]))


def _print_hankel1(self, expr):
return "besselh(%s, 1, %s)" % (self._print(expr.order),
self._print(expr.argument))
Expand Down Expand Up @@ -448,6 +469,9 @@ def _print_Piecewise(self, expr):
lines.append("end")
return "\n".join(lines)

def _print_sinc(self, expr):
return "sinc(%s)" % self._print(expr.args[0]/S.Pi)


def indent_code(self, code):
"""Accepts a string of code or a list of code lines"""
Expand Down
10 changes: 10 additions & 0 deletions sympy/printing/tests/test_octave.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from sympy.functions.special.bessel import (jn, yn, besselj, bessely, besseli,
besselk, hankel1, hankel2, airyai,
airybi, airyaiprime, airybiprime)
from sympy.functions.special.gamma_functions import (lowergamma, uppergamma)
from sympy.utilities.pytest import XFAIL
from sympy.core.compatibility import range

Expand Down Expand Up @@ -245,6 +246,13 @@ def test_octave_piecewise():
expr = Piecewise((x, x < 1), (x**2, x > 1), (sin(x), x > 0))
raises(ValueError, lambda: mcode(expr))

def test_octave_sinc():
from sympy import sinc
expr = octave_code(sinc((x+3)))
assert expr == "sinc((x + 3)/pi)"
expr = octave_code(sinc(pi*(x+3)))
assert expr == "sinc(x + 3)"


def test_octave_piecewise_times_const():
pw = Piecewise((x, x < 1), (x**2, True))
Expand Down Expand Up @@ -363,5 +371,7 @@ def test_specfun():
assert octave_code(airyaiprime(x)) == 'airy(1, x)'
assert octave_code(airybi(x)) == 'airy(2, x)'
assert octave_code(airybiprime(x)) == 'airy(3, x)'
assert octave_code(uppergamma(n, x)) == 'gammainc(x, n, \'upper\')'
assert octave_code(lowergamma(n, x)) == 'gammainc(x, n, \'lower\')'
assert octave_code(jn(n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*besselj(n + 1/2, x)/2'
assert octave_code(yn(n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*bessely(n + 1/2, x)/2'