StevenPollack's profile - activity

2014-06-28 20:12:45 -0500 marked best answer summing over a list of variables?

Hi all,

I'm new to SAGE (I'm trying to move over from Mathematica), and I was wondering if someone could help me define the following function:

$$r_k(x_1, x_2, x_3, x_4) = \left( \sum_{i=k+1}^{4} x_i^2 \right)^{1/2}$$

I figured I could do something like:

var('k,j', domain=NN)
x = [var('x_{index}'.format(index=i)) for i in range(4)]
r(k,x) = sqrt( sum( x[j]^2, j, k, 3 ) )


But this returns the error:

TypeError: unable to convert x (=j) to an integer

Worse yet, when I enter sage:x, it returns x, and forgets x was a list of variables. And to complicate things, when I try

y = [1,2,3]
sum( y[j], j, 0, 1)


I get the error

TypeError: unable to convert x (=j) to an integer

even after I do "clear_vars()" and try everything all over again.

Can anyone tell me what's going on?

2011-08-22 08:57:51 -0500 commented question Using Pydev (eclipse) and Sage?

Just type "sage -python" in your terminal, and it will open up the sage_python interpreter.

2011-08-14 02:37:33 -0500 commented answer when/how to use parallel?

Right. I'm not sure I know what a "linear algorithm" is. Let me edit my question, so I can be more specific.

2011-08-13 18:17:47 -0500 asked a question when/how to use parallel?

So I've scouted around some of the questions posed here that involve the terms parallel or the decorator @parallel and I want to know: is there any documentation out there that introduces novices to parallel computing (in sage)?

At present (i.e. with the parallel? documentation being what it is), I don't really know how (or even when) to take advantage of @parallel.

If I have a dual-core CPU, should I be trying to decorate every function with @parallel? Can someone educate this fool?

Thanks,

Steven

EDIT:

I suppose the best answer here would give an example of situations where @parallel can be of use, and where it cannot. I'm going to throw two examples out there, someone let me know if this is correct. Feel free to add you own, if my examples are two superficial.

1. Computing arc-length of a (smooth) curve parametrized by $t \in [0,2\pi]$

  var('t')
def calcArcLength(gamma):
speed_gamma = sqrt( sum( [ component.diff(t)^2 for component in gamma ]))
return numerical_integral(speed_gamma, 0, 2*pi)[0]

2. Calculating the arc-lengths of a list of smooth curves

@parallel
def makeList(length=1):
li = []
for i in range(length-1):
li.append(generateRandomCurve())
return li

@parallel
def processList(list_of_curves):
arclength_of_curve = []
for curve in list_of_curves:
arclength_of_curve.append(calcArcLength(curve))
return arclength_of_curve


Is it fair to say that @parallel should speed up the time to make and process list_of_curves? Where as it won't speed up the function numerical_integral? 2pi)[0]2pi)[0]2*pi)[0]

2011-08-13 18:02:18 -0500 commented answer Why does @parallel change my outputs?

Thanks. I wish the documentation for @parallel was more clear.

2011-08-13 14:59:33 -0500 commented answer Why does @parallel change my outputs?

Sorry, why are you printing x[0][0][0], why doesn't x[0] suffice?

2011-08-13 14:57:01 -0500 commented answer A way to rotate a tachyon plot

While this isn't my thread, I'd like to thank the poster: this totally answers a question I was wondering about.

2011-08-13 10:54:33 -0500 commented answer problem computing a numeric double integral

So I looked at the link and converted my code to http://sagenb.org/home/TeamSkynet/6/cells/1/__sagenb_servers_sage_notebook_sagenb_sagenb_home_TeamSkynet_6_code_sage112_spyx.html however when I run calcMobiusEnergy, I now get the error "Exception TypeError: 'unable to simplify to float approximation' in '_sagenb_servers_sage_notebook_sagenb_sagenb_home_TeamSkynet_6_code_sage\ 112_spyx_0.arcLength' ignored"... What gives?

2011-08-13 06:38:16 -0500 commented answer problem computing a numeric double integral

So, I just finished running calcMobiusEnergy on (cos(t), sin(t), 0), and found an energy equal to 400.8287... The number is a bit unsettling (it should probably be less that 6*pi + 4), but what's more troublesome is the speed in which the calculation is completed. Running sage from my computer (not the notebook) it took well over 10 minutes to calculate this. Any way to speed this up?

2011-08-13 05:21:44 -0500 commented answer problem computing a numeric double integral

Thanks for the reply. I'll have to play around with your code. As far as I can tell, the circle cannot have mobius energy = 0, since you're summing non-negative quantities, and this would suggest that the integrand is constantly zero (something we know isn't true). P.S. how to do I do that %cython thing when not using the notebook? / Is there any documentation on that I could see? P.P.S. ed should be raised to the -2 inside your final return statement in mobiusEnergyIntegrand.

2011-08-12 14:51:54 -0500 answered a question How do do more complex shell commands in Sage?

This may be entirely newbish, but I was under the impression any terminal command can be run inside the sage console.

For example:

sage: cd ~/Documents
/home/steven/Documents
sage: pwd
'/home/steven/Documents'
sage: mkdir test
sage: ls
1977_001.pdf      do_my_stuff.m        matlab@               test/
batch_script      Dropbox/         MATLAB/               test.pdf
caq_letter_2.odt  FIELDS_stuff/        passport_photo.jpg        test.svg
CAQ_letter.odt    french_passport.jpg  passport_plus_income_tax.pdf
costco_runs.xls   income_tax_1.pdf     research_funding/
sage: rm -r test
rm: remove directory test'? y
sage: ls
1977_001.pdf      do_my_stuff.m        matlab@               test.pdf
batch_script      Dropbox/         MATLAB/               test.svg
caq_letter_2.odt  FIELDS_stuff/        passport_photo.jpg
CAQ_letter.odt    french_passport.jpg  passport_plus_income_tax.pdf
costco_runs.xls   income_tax_1.pdf     research_funding/


I don't really know how echo works, so I can't really help you there.

2011-08-12 14:42:58 -0500 asked a question problem computing a numeric double integral

So, I'd like to compute

$$\int_{0}^{2\pi} \int_{0}^{2\pi} \left( \frac{1}{\left| \gamma(u) - \gamma(v)\right|^2} - \frac{1}{D(\gamma(u), \gamma(v))^2}\right) \left| \gamma'(u) \right| \left| \gamma'(v)\right| du dv$$ where $\gamma : [0,2\pi] \to \mathbb{R}^3$ is a curve, and $D(\gamma(u), \gamma(v))$ is the length of the shortest path (on the curve) between $\gamma(u)$ and $\gamma(v)$ (the geodesic arclength).

Here's my code:

def speed(gamma):
"""returns norm{gamma'}, the speed of the curve gamma(t)"""
return gamma.diff(t).norm().function(t)

def arcLength(gamma,a,b):
"""Returns the arc-length of a curve, gamma = f(t), from t=a to t=b """
return abs(numerical_integral(speed(gamma), a, b)[0])

def geodesicArcLength(gamma,a,b):
"""finds the smallest distance between points t=a and t=b on a curve, gamma(t), t \in [0,2*pi]"""
totalLength = arcLength(gamma,0,2*pi)
if a <= b:
lengthOfPath1 = arcLength(gamma,a,b)
else:
lengthOfPath1 = arcLength(gamma,b,a)

if lengthOfPath1 <= (totalLength/2.0):
return lengthOfPath1
else:

def euclideanDistance(gamma,a,b):
"""finds the euclidean distance on gamma(t), a curve, between gamma(a) and gamma(b)"""
return norm(gamma(a) - gamma(b))

def mobiusEnergyIntegrand(gamma,u,v):
return speed(gamma)(u)*speed(gamma)(v)/(euclideanDistance(gamma,u,v)^(-2) - geodesicArcLength(gamma,u,v)^(-2))

def calcMobiusEnergy(gamma):
return numerical_integral( lambda v: numerical_integral( lambda u: mobiusEnergyIntegrand(gamma,u,v) , (u,0,2*pi) )[0] , (v,0,2*pi) )


However, when I try and test this on something simple like

sage: circle(t) = (cos(t), sin(t), 0)
sage: calcMobiusEnergy(circle)


I get

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/steven/sage-4.7/local/lib/python2.6/site-packages/sage/all_cmdline.pyc in <module>()

/home/steven/sage-4.7/local/lib/python2.6/site-packages/sage/all_cmdline.pyc in calcMobiusEnergy(gamma)

/home/steven/sage-4.7/local/lib/python2.6/site-packages/sage/gsl/integration.so in sage.gsl.integration.numerical_integral (sage/gsl/integration.c:2432)()

/home/steven/sage-4.7/local/lib/python2.6/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.__float__ (sage/symbolic/expression.cpp:5543)()

TypeError: unable to simplify to float approximation


Is my entire strategy just not right? Does anyone have any ideas on how to make this work? I used the response to this old question as a model for the function "calcMobiusEnergy".

Thanks,

Steven

2011-08-03 11:52:12 -0500 commented answer Knot Theory and Sage?

I've checked out the Knot Atlas, but the fact that I needed to use mathematica to harness their software kept me from seriously considering anything over there. I'm surprised that no one has tried contacting the Knot Atlas people regarding a Sage port. As far as I know the two admin's are U of Toronto professors, right? So they shouldn't be that opposed to an open-source frame-work to their project... Thanks for the reply, by the way.

2011-08-02 09:24:37 -0500 asked a question Knot Theory and Sage?

I was wondering what kind of Knot Theory tools Sage has? I've done a couple google searches, as well as a doc search in the Sage reference manual; everything's turned up bunk.

Does anyone know of any Knot Theory tools in sage? In particular, knot tables?

Thanks,

Steven

2011-07-20 09:02:11 -0500 marked best answer summing over a list of variables?

There are two unrelated problems here. One - assigning a function also reassigns the variables used to define it:

sage: x = 4
sage: x
4
sage: f(x) = x^2
sage: f
x |--> x^2
sage: x
x


This causes your x to dissapear. You just need to give r's variables a different name.

Second - y[j] cannot function as a symbolic expression - I believe unlike Mathematica sage does not implement "indexing" as a symbolic expression (see also here). So 'sum( y[j], j, 0, 1)' cannot work. The way to sum over a list y is simply sum(y), but this still won't solve your first problem. You can do something like

sage: r = lambda k:sqrt(sum([var('x_%d'%(k+i)) for i in range(4)]))
sage: r(2)
sqrt(x_2 + x_3 + x_4 + x_5)
sage: r(7)
sqrt(x_10 + x_7 + x_8 + x_9)


But r(k) will not work for k a variable.

2011-07-20 08:36:31 -0500 edited question Using Pydev (eclipse) and Sage?

Hi,

I've been googling around to find a way to set up pydev and sage, and have had nothing but the worst of luck.

sage 4.7 is located in /home/steven/sage-4.7, and I've created a "sage_python" python interpreter who's location is "/home/steven/sage-4.7/local/bin/python".

I've set the following environment variables:

SAGE_ROOT = /home/steven/sage-4.7
LD_LIBRARY_PATH = $SAGE_ROOT/local/lib  Now, when I run the following script (test.py): #!/usr/bin/env sage -python import sys from sage.all import * print 2  When I run the script with sage_python as the interpreter I get the following error: Traceback (most recent call last): File "/home/steven/code/whateva/test.py", line 4, in <module> from sage.all import * File "/home/steven/sage-4.7/local/lib/python2.6/site-packages/sage/ all.py", line 70, in <module> from sage.ext.c_lib import _init_csage, sig_on_count ImportError: libcsage.so: cannot open shared object file: No such file or directorydirectory Can anyone help me out with this? Thanks, Steven p.s. I've noticed this post on the sage support google groups, but even when I open eclipse via $ sage -sh
\$ eclipse &
`

I get the same error.