Skip to content

Commit

Permalink
* Make comment on behavior of sum.
Browse files Browse the repository at this point in the history
* update a notebook.
  • Loading branch information
jlapeyre committed Nov 20, 2016
1 parent 9420265 commit c4fa6b9
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 26 deletions.
130 changes: 105 additions & 25 deletions examples/Interface with Julia.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@
{
"data": {
"text/latex": [
"$$ \\left[ 0.08162695,7.485470860550343 \\right] $$"
"$$ \\left[ 0.083769388,7.485470860550343 \\right] $$"
],
"text/plain": [
"L\"$$ \\left[ 0.08162695,7.485470860550343 \\right] $$\""
"L\"$$ \\left[ 0.083769388,7.485470860550343 \\right] $$\""
]
},
"execution_count": 4,
Expand Down Expand Up @@ -120,10 +120,10 @@
{
"data": {
"text/latex": [
"$$ \\left[ 0.057963461,7.485470860550343 \\right] $$"
"$$ \\left[ 0.062642894,7.485470860550343 \\right] $$"
],
"text/plain": [
"L\"$$ \\left[ 0.057963461,7.485470860550343 \\right] $$\""
"L\"$$ \\left[ 0.062642894,7.485470860550343 \\right] $$\""
]
},
"execution_count": 5,
Expand Down Expand Up @@ -155,10 +155,10 @@
{
"data": {
"text/latex": [
"$$ 1.4082483790952374 $$"
"$$ 1.337252841479514 $$"
],
"text/plain": [
"L\"$$ 1.4082483790952374 $$\""
"L\"$$ 1.337252841479514 $$\""
]
},
"execution_count": 6,
Expand Down Expand Up @@ -200,10 +200,10 @@
{
"data": {
"text/latex": [
"$$ \\left[ 0.000371906,7.485470860550343 \\right] $$"
"$$ \\left[ 0.000293749,7.485470860550343 \\right] $$"
],
"text/plain": [
"L\"$$ \\left[ 0.000371906,7.485470860550343 \\right] $$\""
"L\"$$ \\left[ 0.000293749,7.485470860550343 \\right] $$\""
]
},
"execution_count": 8,
Expand Down Expand Up @@ -235,10 +235,10 @@
{
"data": {
"text/latex": [
"$$ \\left[ 219.4827456400273,155.85513812630072 \\right] $$"
"$$ \\left[ 285.1733554837634,213.25313107448878 \\right] $$"
],
"text/plain": [
"L\"$$ \\left[ 219.4827456400273,155.85513812630072 \\right] $$\""
"L\"$$ \\left[ 285.1733554837634,213.25313107448878 \\right] $$\""
]
},
"execution_count": 9,
Expand Down Expand Up @@ -284,6 +284,86 @@
"jfunc([a+b, c+d],[u+v,y+z])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Evaluating Symata expressions from Julia\n",
"\n",
"The macro `@sym` evaluates Symata code while in Julia.\n",
"\n",
"First, we switch to Julia mode."
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Julia();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a Symata expression and bind it to the Julia variable `expr`."
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
":a + :b"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"expr = @sym a + b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are julia functions corresponding to many Symata expression heads."
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
":a^2 + :b^2 + 2:a*:b"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Expand(expr^2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -398,10 +478,10 @@
{
"data": {
"text/latex": [
"$$ 1.47948546680267e9 $$"
"$$ 1.479677621013672e9 $$"
],
"text/plain": [
"L\"$$ 1.47948546680267e9 $$\""
"L\"$$ 1.479677621013672e9 $$\""
]
},
"execution_count": 15,
Expand Down Expand Up @@ -479,10 +559,10 @@
{
"data": {
"text/latex": [
"$$ \\left[ 0.000314142,7.485470860550343 \\right] $$"
"$$ \\left[ 0.000375214,7.485470860550343 \\right] $$"
],
"text/plain": [
"L\"$$ \\left[ 0.000314142,7.485470860550343 \\right] $$\""
"L\"$$ \\left[ 0.000375214,7.485470860550343 \\right] $$\""
]
},
"execution_count": 17,
Expand Down Expand Up @@ -513,10 +593,10 @@
{
"data": {
"text/latex": [
"$$ 1.1838786281363205 $$"
"$$ 0.7828839009205413 $$"
],
"text/plain": [
"L\"$$ 1.1838786281363205 $$\""
"L\"$$ 0.7828839009205413 $$\""
]
},
"execution_count": 18,
Expand Down Expand Up @@ -545,10 +625,10 @@
{
"data": {
"text/latex": [
"$$ \\left[ 4.5826e-5,7.485470860550343 \\right] $$"
"$$ \\left[ 5.1017e-5,7.485470860550343 \\right] $$"
],
"text/plain": [
"L\"$$ \\left[ 4.5826e-5,7.485470860550343 \\right] $$\""
"L\"$$ \\left[ 5.1017e-5,7.485470860550343 \\right] $$\""
]
},
"execution_count": 19,
Expand Down Expand Up @@ -578,10 +658,10 @@
{
"data": {
"text/latex": [
"$$ 6.855104089381574 $$"
"$$ 7.354685693004293 $$"
],
"text/plain": [
"L\"$$ 6.855104089381574 $$\""
"L\"$$ 7.354685693004293 $$\""
]
},
"execution_count": 20,
Expand Down Expand Up @@ -797,27 +877,27 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/latex": [
"$$ \\left[ \\text{Array{Float64,1}},1000.0 \\right] $$"
"$$ \\left[ \\text{Array{Float64,1}},1.0 \\right] $$"
],
"text/plain": [
"L\"$$ \\left[ \\text{Array{Float64,1}},1000.0 \\right] $$\""
"L\"$$ \\left[ \\text{Array{Float64,1}},1.0 \\right] $$\""
]
},
"execution_count": 28,
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[Head(y4), y4[-1]]"
"[Head(y4), y4[1]]"
]
},
{
Expand Down
7 changes: 6 additions & 1 deletion sjtest/output_test.sj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ T ToString(0/0) == "Indeterminate"
### HoldForm

f(x_) := HoldForm(x^2)
T ToString(Sum(f(i), [i,1,10])) == "1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 6^2 + 7^2 + 8^2 + 9^2 + 10^2"

## FIXME. The first expression is what we want. If we don't evaluate the summand before doing
## the sum, we do get the first version.
#T ToString(Sum(f(i), [i,1,10])) == "1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 6^2 + 7^2 + 8^2 + 9^2 + 10^2"
T ToString(Sum(f(i), [i,1,10])) == "1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100"

T Map(ReleaseHold, Sum(f(i), [i,1,10])) == 385

### Compound head
Expand Down
1 change: 1 addition & 0 deletions src/sympy_application.jl
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ sums over `x` from `a` to `b`.
apprules(mx::Mxpr{:Sum}) = do_Sum(mx,margs(mx)...)

function do_Sum(mx::Mxpr{:Sum}, expr, varspecs...)
## FIXME: only evaluate expr here if the range of the sum is infinite.
pymx = sjtopy(doeval(expr))
pyvarspecs = varspecs_to_tuples_of_sympy(reverse(collect(varspecs))) # Symata and Mma use the same convention for position of inner loop
pysum = sympy[:summation](pymx,pyvarspecs...)
Expand Down

0 comments on commit c4fa6b9

Please sign in to comment.