本文整理汇总了Python中sympy.jscode函数的典型用法代码示例。如果您正苦于以下问题:Python jscode函数的具体用法?Python jscode怎么用?Python jscode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了jscode函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_jscode_Pow
def test_jscode_Pow():
g = implemented_function('g', Lambda(x, 2*x))
assert jscode(x**3) == "Math.pow(x, 3)"
assert jscode(x**(y**3)) == "Math.pow(x, Math.pow(y, 3))"
assert jscode(1/(g(x)*3.5)**(x - y**x)/(x**2 + y)) == \
"Math.pow(3.5*2*x, -x + Math.pow(y, x))/(Math.pow(x, 2) + y)"
assert jscode(x**-1.0) == '1/x'
开发者ID:abhi98khandelwal,项目名称:sympy,代码行数:7,代码来源:test_jscode.py
示例2: test_jscode_Pow
def test_jscode_Pow():
assert jscode(x ** 3) == "Math.pow(x, 3)"
assert jscode(x ** (y ** 3)) == "Math.pow(x, Math.pow(y, 3))"
assert (
jscode(1 / (g(x) * 3.5) ** (x - y ** x) / (x ** 2 + y))
== "Math.pow(3.5*g(x), -x + Math.pow(y, x))/(Math.pow(x, 2) + y)"
)
assert jscode(x ** -1.0) == "1/x"
开发者ID:cbr-washington-edu,项目名称:branch,代码行数:8,代码来源:test_jscode.py
示例3: test_MatrixElement_printing
def test_MatrixElement_printing():
# test cases for issue #11821
A = MatrixSymbol("A", 1, 3)
B = MatrixSymbol("B", 1, 3)
C = MatrixSymbol("C", 1, 3)
assert(jscode(A[0, 0]) == "A[0]")
assert(jscode(3 * A[0, 0]) == "3*A[0]")
F = C[0, 0].subs(C, A - B)
assert(jscode(F) == "((-1)*B + A)[0]")
开发者ID:abhi98khandelwal,项目名称:sympy,代码行数:11,代码来源:test_jscode.py
示例4: test_jscode_inline_function
def test_jscode_inline_function():
x = symbols("x")
g = implemented_function("g", Lambda(x, 2 * x))
assert jscode(g(x)) == "2*x"
g = implemented_function("g", Lambda(x, 2 * x / Catalan))
assert jscode(g(x)) == "var Catalan = %s;\n2*x/Catalan" % Catalan.n()
A = IndexedBase("A")
i = Idx("i", symbols("n", integer=True))
g = implemented_function("g", Lambda(x, x * (1 + x) * (2 + x)))
assert jscode(g(A[i]), assign_to=A[i]) == (
"for (var i=0; i<n; i++){\n" " A[i] = A[i]*(1 + A[i])*(2 + A[i]);\n" "}"
)
开发者ID:cbr-washington-edu,项目名称:branch,代码行数:12,代码来源:test_jscode.py
示例5: test_jscode_inline_function
def test_jscode_inline_function():
x = symbols('x')
g = implemented_function('g', Lambda(x, 2*x))
assert jscode(g(x)) == "2*x"
g = implemented_function('g', Lambda(x, 2*x/Catalan))
assert jscode(g(x)) == "var Catalan = %s;\n2*x/Catalan" % Catalan.n()
A = IndexedBase('A')
i = Idx('i', symbols('n', integer=True))
g = implemented_function('g', Lambda(x, x*(1 + x)*(2 + x)))
assert jscode(g(A[i]), assign_to=A[i]) == (
"for (var i=0; i<n; i++){\n"
" A[i] = (A[i] + 1)*(A[i] + 2)*A[i];\n"
"}"
)
开发者ID:abhi98khandelwal,项目名称:sympy,代码行数:14,代码来源:test_jscode.py
示例6: test_jscode_loops_addfactor
def test_jscode_loops_addfactor():
from sympy.tensor import IndexedBase, Idx
from sympy import symbols
n, m, o, p = symbols("n m o p", integer=True)
a = IndexedBase("a")
b = IndexedBase("b")
c = IndexedBase("c")
y = IndexedBase("y")
i = Idx("i", m)
j = Idx("j", n)
k = Idx("k", o)
l = Idx("l", p)
s = (
"for (var i=0; i<m; i++){\n"
" y[i] = 0;\n"
"}\n"
"for (var i=0; i<m; i++){\n"
" for (var j=0; j<n; j++){\n"
" for (var k=0; k<o; k++){\n"
" for (var l=0; l<p; l++){\n"
" y[i] = (a[i*n*o*p + j*o*p + k*p + l] + b[i*n*o*p + j*o*p + k*p + l])*c[j*o*p + k*p + l] + y[i];\n"
" }\n"
" }\n"
" }\n"
"}"
)
c = jscode((a[i, j, k, l] + b[i, j, k, l]) * c[j, k, l], assign_to=y[i])
assert c == s
开发者ID:cbr-washington-edu,项目名称:branch,代码行数:30,代码来源:test_jscode.py
示例7: jsify_expr
def jsify_expr(expr):
clamp = Function("clamp")
bottom_clamp = Function("bottom")
x = Wild("x")
# Prevent NaNs on inverse trig functions
expr = expr.replace(asin, lambda x: asin(clamp(x, -1, 1)))
expr = expr.replace(acos, lambda x: acos(clamp(x, -1, 1)))
# Prevent NaNs on sqrts
expr = expr.replace(sqrt(x + 1),
sqrt(bottom_clamp(x + 1, 0)))
js_expr = jscode(expr, user_functions = {
"clamp": "THREE.Math.clamp",
"bottom": "THREE.Math.clampBottom"
})
# Convert all matrix references for compatibility with
# three.js
atoms = expr.atoms(Symbol)
matrices = set([])
for atom in atoms:
matrix_name = re.findall("([a-zA-Z]+)_\d,\d", atom.name)
if len(matrix_name) > 0:
matrices.add(matrix_name[0])
for matrix in matrices:
js_expr = subs_matrix_elements(js_expr, matrix)
return js_expr
开发者ID:TJSomething,项目名称:ncb,代码行数:25,代码来源:build_motion.py
示例8: test_jscode_loops_multiple_contractions
def test_jscode_loops_multiple_contractions():
from sympy.tensor import IndexedBase, Idx
from sympy import symbols
n, m, o, p = symbols('n m o p', integer=True)
a = IndexedBase('a')
b = IndexedBase('b')
y = IndexedBase('y')
i = Idx('i', m)
j = Idx('j', n)
k = Idx('k', o)
l = Idx('l', p)
s = (
'for (var i=0; i<m; i++){\n'
' y[i] = 0;\n'
'}\n'
'for (var i=0; i<m; i++){\n'
' for (var j=0; j<n; j++){\n'
' for (var k=0; k<o; k++){\n'
' for (var l=0; l<p; l++){\n'
' y[i] = y[i] + b[j*o*p + k*p + l]*a[i*n*o*p + j*o*p + k*p + l];\n'
' }\n'
' }\n'
' }\n'
'}'
)
c = jscode(b[j, k, l]*a[i, j, k, l], assign_to=y[i])
assert c == s
开发者ID:Acebulf,项目名称:sympy,代码行数:28,代码来源:test_jscode.py
示例9: eval_graph
def eval_graph(evaluator, variable):
from sympy.plotting.plot import LineOver1DRangeSeries
func = evaluator.eval("input_evaluated")
free_symbols = sympy.sympify(func).free_symbols
if len(free_symbols) != 1 or variable not in free_symbols:
raise ValueError("Cannot graph function of multiple variables")
try:
series = LineOver1DRangeSeries(func, (variable, -10, 10), nb_of_points=200)
# returns a list of [[x,y], [next_x, next_y]] pairs
series = series.get_segments()
except TypeError:
raise ValueError("Cannot graph function")
xvalues = []
yvalues = []
for point in series:
xvalues.append(point[0][0])
yvalues.append(point[0][1])
xvalues.append(series[-1][1][0])
yvalues.append(series[-1][1][1])
return {
'function': sympy.jscode(sympy.sympify(func)),
'variable': repr(variable),
'xvalues': json.dumps(xvalues),
'yvalues': json.dumps(yvalues)
}
开发者ID:joaomfranco,项目名称:sympy_gamma,代码行数:28,代码来源:resultsets.py
示例10: test_jscode_loops_addfactor
def test_jscode_loops_addfactor():
from sympy.tensor import IndexedBase, Idx
from sympy import symbols
n, m, o, p = symbols('n m o p', integer=True)
a = IndexedBase('a')
b = IndexedBase('b')
c = IndexedBase('c')
y = IndexedBase('y')
i = Idx('i', m)
j = Idx('j', n)
k = Idx('k', o)
l = Idx('l', p)
s = (
'for (var i=0; i<m; i++){\n'
' y[i] = 0;\n'
'}\n'
'for (var i=0; i<m; i++){\n'
' for (var j=0; j<n; j++){\n'
' for (var k=0; k<o; k++){\n'
' for (var l=0; l<p; l++){\n'
' y[i] = (a[%s] + b[%s])*c[%s] + y[i];\n' % (i*n*o*p + j*o*p + k*p + l, i*n*o*p + j*o*p + k*p + l, j*o*p + k*p + l) +\
' }\n'
' }\n'
' }\n'
'}'
)
c = jscode((a[i, j, k, l] + b[i, j, k, l])*c[j, k, l], assign_to=y[i])
assert c == s
开发者ID:abhi98khandelwal,项目名称:sympy,代码行数:29,代码来源:test_jscode.py
示例11: test_jscode_boolean
def test_jscode_boolean():
assert jscode(x & y) == "x && y"
assert jscode(x | y) == "x || y"
assert jscode(~x) == "!x"
assert jscode(x & y & z) == "x && y && z"
assert jscode(x | y | z) == "x || y || z"
assert jscode((x & y) | z) == "z || x && y"
assert jscode((x | y) & z) == "z && (x || y)"
开发者ID:cbr-washington-edu,项目名称:branch,代码行数:8,代码来源:test_jscode.py
示例12: test_jscode_Piecewise
def test_jscode_Piecewise():
p = jscode(Piecewise((x, x < 1), (x ** 2, True)))
s = """\
if (x < 1) {
x
}
else {
Math.pow(x, 2)
}\
"""
assert p == s
开发者ID:cbr-washington-edu,项目名称:branch,代码行数:11,代码来源:test_jscode.py
示例13: eval_graph
def eval_graph(evaluator, variable):
from sympy.plotting.plot import LineOver1DRangeSeries
func = evaluator.eval("input_evaluated")
series = LineOver1DRangeSeries(func, (variable, -10, 10), nb_of_points=200)
series = series.get_points()
return {
'function': sympy.jscode(sympy.sympify(func)),
'variable': repr(variable),
'xvalues': json.dumps(series[0].tolist()),
'yvalues': json.dumps(series[1].tolist())
}
开发者ID:catchmrbharath,项目名称:sympy_gamma,代码行数:11,代码来源:resultsets.py
示例14: test_jscode_Piecewise_deep
def test_jscode_Piecewise_deep():
p = jscode(2*Piecewise((x, x < 1), (x**2, True)))
s = \
"""\
2*((x < 1) ? (
x
)
: (
Math.pow(x, 2)
))\
"""
assert p == s
开发者ID:abhi98khandelwal,项目名称:sympy,代码行数:12,代码来源:test_jscode.py
示例15: test_jscode_Piecewise_deep
def test_jscode_Piecewise_deep():
p = jscode(2*Piecewise((x, x < 1), (x**2, True)))
s = \
"""\
2*if (x < 1) {
x
}
else {
Math.pow(x, 2)
}\
"""
assert p == s
开发者ID:Acebulf,项目名称:sympy,代码行数:12,代码来源:test_jscode.py
示例16: test_dummy_loops
def test_dummy_loops():
i, m = symbols('i m', integer=True, cls=Dummy)
x = IndexedBase('x')
y = IndexedBase('y')
i = Idx(i, m)
expected = (
'for (var i_%(icount)i=0; i_%(icount)i<m_%(mcount)i; i_%(icount)i++){\n'
' y[i_%(icount)i] = x[i_%(icount)i];\n'
'}'
) % {'icount': i.label.dummy_index, 'mcount': m.dummy_index}
code = jscode(x[i], assign_to=y[i])
assert code == expected
开发者ID:abhi98khandelwal,项目名称:sympy,代码行数:13,代码来源:test_jscode.py
示例17: test_Matrix_printing
def test_Matrix_printing():
# Test returning a Matrix
mat = Matrix([x*y, Piecewise((2 + x, y>0), (y, True)), sin(z)])
A = MatrixSymbol('A', 3, 1)
assert jscode(mat, A) == (
"A[0] = x*y;\n"
"if (y > 0) {\n"
" A[1] = x + 2;\n"
"}\n"
"else {\n"
" A[1] = y;\n"
"}\n"
"A[2] = Math.sin(z);")
# Test using MatrixElements in expressions
expr = Piecewise((2*A[2, 0], x > 0), (A[2, 0], True)) + sin(A[1, 0]) + A[0, 0]
assert jscode(expr) == (
"((x > 0) ? (\n"
" 2*A[2]\n"
")\n"
": (\n"
" A[2]\n"
")) + Math.sin(A[1]) + A[0]")
# Test using MatrixElements in a Matrix
q = MatrixSymbol('q', 5, 1)
M = MatrixSymbol('M', 3, 3)
m = Matrix([[sin(q[1,0]), 0, cos(q[2,0])],
[q[1,0] + q[2,0], q[3, 0], 5],
[2*q[4, 0]/q[1,0], sqrt(q[0,0]) + 4, 0]])
assert jscode(m, M) == (
"M[0] = Math.sin(q[1]);\n"
"M[1] = 0;\n"
"M[2] = Math.cos(q[2]);\n"
"M[3] = q[1] + q[2];\n"
"M[4] = q[3];\n"
"M[5] = 5;\n"
"M[6] = 2*q[4]/q[1];\n"
"M[7] = Math.sqrt(q[0]) + 4;\n"
"M[8] = 0;")
开发者ID:abhi98khandelwal,项目名称:sympy,代码行数:38,代码来源:test_jscode.py
示例18: test_jscode_Piecewise
def test_jscode_Piecewise():
expr = Piecewise((x, x < 1), (x**2, True))
p = jscode(expr)
s = \
"""\
((x < 1) ? (
x
)
: (
Math.pow(x, 2)
))\
"""
assert p == s
assert jscode(expr, assign_to="c") == (
"if (x < 1) {\n"
" c = x;\n"
"}\n"
"else {\n"
" c = Math.pow(x, 2);\n"
"}")
# Check that Piecewise without a True (default) condition error
expr = Piecewise((x, x < 1), (x**2, x > 1), (sin(x), x > 0))
raises(ValueError, lambda: jscode(expr))
开发者ID:abhi98khandelwal,项目名称:sympy,代码行数:23,代码来源:test_jscode.py
示例19: test_jscode_loops_multiple_terms
def test_jscode_loops_multiple_terms():
from sympy.tensor import IndexedBase, Idx
from sympy import symbols
n, m, o, p = symbols('n m o p', integer=True)
a = IndexedBase('a')
b = IndexedBase('b')
c = IndexedBase('c')
y = IndexedBase('y')
i = Idx('i', m)
j = Idx('j', n)
k = Idx('k', o)
s0 = (
'for (var i=0; i<m; i++){\n'
' y[i] = 0;\n'
'}\n'
)
s1 = (
'for (var i=0; i<m; i++){\n'
' for (var j=0; j<n; j++){\n'
' for (var k=0; k<o; k++){\n'
' y[i] = b[j]*b[k]*c[%s] + y[i];\n' % (i*n*o + j*o + k) +\
' }\n'
' }\n'
'}\n'
)
s2 = (
'for (var i=0; i<m; i++){\n'
' for (var k=0; k<o; k++){\n'
' y[i] = a[%s]*b[k] + y[i];\n' % (i*o + k) +\
' }\n'
'}\n'
)
s3 = (
'for (var i=0; i<m; i++){\n'
' for (var j=0; j<n; j++){\n'
' y[i] = a[%s]*b[j] + y[i];\n' % (i*n + j) +\
' }\n'
'}\n'
)
c = jscode(
b[j]*a[i, j] + b[k]*a[i, k] + b[j]*b[k]*c[i, j, k], assign_to=y[i])
assert (c == s0 + s1 + s2 + s3[:-1] or
c == s0 + s1 + s3 + s2[:-1] or
c == s0 + s2 + s1 + s3[:-1] or
c == s0 + s2 + s3 + s1[:-1] or
c == s0 + s3 + s1 + s2[:-1] or
c == s0 + s3 + s2 + s1[:-1])
开发者ID:abhi98khandelwal,项目名称:sympy,代码行数:48,代码来源:test_jscode.py
示例20: eval_graph
def eval_graph(evaluator, components, parameters=None):
if parameters is None:
parameters = {}
variable = components['variable']
xmin, xmax = parameters.get('xmin', -10), parameters.get('xmax', 10)
from sympy.plotting.plot import LineOver1DRangeSeries
func = evaluator.get("input_evaluated")
free_symbols = func.free_symbols
if len(free_symbols) != 1 or variable not in free_symbols:
raise ValueError("Cannot graph function of multiple variables")
try:
series = LineOver1DRangeSeries(
func, (variable, xmin, xmax),
nb_of_points=150)
# returns a list of [[x,y], [next_x, next_y]] pairs
series = series.get_segments()
except TypeError:
raise ValueError("Cannot graph function")
xvalues = []
yvalues = []
def limit_y(y):
CEILING = 1e8
if y > CEILING:
y = CEILING
if y < -CEILING:
y = -CEILING
return y
for point in series:
xvalues.append(point[0][0])
yvalues.append(limit_y(point[0][1]))
xvalues.append(series[-1][1][0])
yvalues.append(limit_y(series[-1][1][1]))
return {
'function': sympy.jscode(sympy.sympify(func)),
'variable': repr(variable),
'xvalues': json.dumps(xvalues),
'yvalues': json.dumps(yvalues)
}
开发者ID:Jose-,项目名称:sympy_gamma,代码行数:46,代码来源:resultsets.py
注:本文中的sympy.jscode函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论