ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 25 Jun 2020 13:05:25 +0200List of variables as function argumentshttps://ask.sagemath.org/question/52218/list-of-variables-as-function-arguments/ I would like to define a function that takes an arbitrary long list of variables as input and some complicated expression of my variables as my output, but I just don't seem to be able to make it work.
Something along the line of:
var('x_1,x_2,x_3')
long_list_of_variables = [x_1, x_2,x_3]
complicated_expression=(x_1^2+x_2^2-x_3)^2
f(tuple(long_list_of_variables))=complicated_expression
But with much more variables.
I've tried different variations of this but I always seem to end up with the 'can't assign to function call'-error.
Are there any way to achieve what I'm trying to do?ubeThu, 25 Jun 2020 13:05:25 +0200https://ask.sagemath.org/question/52218/Is there a way do declare a python function or fast_callable fo a cython function ?https://ask.sagemath.org/question/40252/is-there-a-way-do-declare-a-python-function-or-fast_callable-fo-a-cython-function/I want to get efficient code for a repetitive computation on a "grid" of arguments, using a runtime-defined function (passed as an argument). Basically :
def myfunc(f, somerange, someotherrange):
initstorage()
for x in somerange:
for y in someotherrange:
store(x,y,dosomething(f,x,y))
return storage
In order to accelerate things, I'd like to write myfunc in cython.
I can safely assume that my numerical arguments (`x ` and `y`) will be coerced to double, so I can further accelerate things gy declaring them as typed. The problem is the function argument `f`. I can test the nature of `f` (e. g. before `initstorage()` and set `dosomething` to a special_case function according to this nature.
There are basically four cases :
* a symbolic expression : it seems that, in most cases, it pays to generate a `fast_callable`.
* a `fast_callable`.
* a Python function or a lambda expression.
* a Cython function declared as `cpdef`, or a C/C++ function suitably imported.
For this last case, I know the solution : it is enough to declare the relevant function signature with `ctypedef`, and use this type in the declaration of my function :
ctypedef double (*mytype)(double, double)
and use it to declare `myfunc` :
cpdef myfunc(mytype, double, double): #see above...
with the benefit of acceleration due to static typing.
Can something analogous be done in the three other cases (or possibly in the `fast_callable` and Python function cases, an expression being convertible to the `fast_callable` case) ?Emmanuel CharpentierWed, 20 Dec 2017 11:55:01 +0100https://ask.sagemath.org/question/40252/Executing Sage with arguments from C with exec familyhttps://ask.sagemath.org/question/33795/executing-sage-with-arguments-from-c-with-exec-family/Hi, I'm writing a piece of software that lets me distribute executions of the type *same program-multiple data* in a computing server through PVM. This software has a module for Maple executions, one for C, one for Python and one for Pari. Now I'm trying to write the code for a Sage module.
The idea is that i have a master who sends tasks to several slaves, each task consists on executing the same Sage script but using different values for the variables `taskId` and `taskArgs`. The slave forks a process and executes a chunk such as this one:
// NULL-terminated array of strings
char **args;
int nargs=2;
args = (char**)malloc((nargs+1)*sizeof(char*));
// Do not malloc for NULL
for (i=0;i<nargs;i++)
args[i] = malloc(BUFFER_SIZE);
// Fill up the array with strings
sprintf(args[0],"sage");
sprintf(args[1],"-c \"taskId=%d;taskArgs=[%s];load('%s');\"",taskNumber,arguments,inp_programFile);
args[2] = NULL;
// Call the execution and check for errors
err = execvp(args[0],args);
perror("ERROR:: child Sage process");
exit(err);
For example, an instance of this code would be executing
`sage -c "taskId=71;taskArgs=[0,73,74,0];load('test.sage')"`,
where *test.sage* prints the arguments or whatever.
Now comes the question. If i execute the previous command from the command line i get the expected result (the two prints). However, if i execute my C program (which works corectly for Maple, C, Python and Pari using a very similar approach) i get this error:
`sage-run received unknown option: -c "taskId=71;taskArgs:=[0,73,74,0];load('test.sage')"`
I have already tried changing the double quotes `"` to single quotes `'`, using only a single, very simple argument from C (e.g. `sage -c "print(1);"`), all to no avail.
Am I missing something? How can the same command work from the command line but not when i use `execvp` from within C?
Thanks in advanced for any help!
----------
(I know I could adapt my Python module to read Sage scripts, but i want a *standalone* module for Sage because the Python one uses `sys.args` and I'd like to keep the Sage scripts as simple as possible for the users of the software)osrWed, 15 Jun 2016 14:16:59 +0200https://ask.sagemath.org/question/33795/Plotting issue, number of arguments errorhttps://ask.sagemath.org/question/26429/plotting-issue-number-of-arguments-error/Hey all,
I have a question that's probably been asked before (tough I couldn't find it here)
I'm trying to plot these two functions of d. Area is a function of d and some other functions of d. I could swear I had this code working a few months ago but now I get the error:
***
*File "", line 1, in <module>
File "/tmp/tmp0fBGGb/___code___.py", line 21, in <module>
plot1 = plot(Area, R1-R2, limit, color='red')
File "sage/symbolic/expression.pyx", line 4382, in sage.symbolic.expression.Expression.__call__ (build/cythonized/sage/symbolic/expression.cpp:24669)
File "sage/symbolic/ring.pyx", line 760, in sage.symbolic.ring.SymbolicRing._call_element_ (build/cythonized/sage/symbolic/ring.cpp:8970)
ValueError: the number of arguments must be less than or equal to 1*
***
Does anyone have any ideas what this could be?
(Using sageNB and 6.4.1)
var('x, y, z, R1, R2, d, a, s1, s2, Area, limit, Area2')
R1 = 6
R2 = 4
a = (1/d)*sqrt( (-d+R1+R2)*(d-R1+R2)*(d+R1-R2)*(d+R1+R2) )
s1 = (a + 2*R1)/2
s2 = (a + 2*R2)/2
limit = sqrt(R1**2 - (R2/2)**2) + (R1 - R2)
Area = [((R1**2)*arcsin(a/(2*R1))) - sqrt(s1*(s1-a)*((s1-R1)^2)) + (R2**2)*(pi - arcsin(a/(2*R2))) + sqrt(s2*(s2-a)*((s2-R2)**2))]
plot1 = plot(Area, d, R1-R2, limit, color='red')
Area2 = ((R1**2)*arcsin(a/(2*R1))) - sqrt(s1*(s1-a)*((s1-R1)^2)) + (R2**2)*(arcsin(a/(2*R2))) - sqrt(s2*(s2-a)*((s2-R2)**2))
plot2 = plot(Area2, d, limit, R1+R2, color='green')
show(plot1)
ajk_birdmanThu, 02 Apr 2015 19:19:22 +0200https://ask.sagemath.org/question/26429/How to find the full argument list of a built-in function?https://ask.sagemath.org/question/25609/how-to-find-the-full-argument-list-of-a-built-in-function/My first use of Sage is utilizing its fantastic 3D plotting capabilities with Jmol. I am visualizing 3D curves from data points via line3d() and than using the show() method on the graphics objects. line3d? and show? give a short list of arguments such as color, thickness, radius for line3d, and frame for show. I know there are many more arguments, for example corner_cutoff for smoothing, but I only stumble upon them while browsing the Sage documentation. So far, I haven't found a source to list all the arguments for line3d or similar functions. I understand that these are Python wrappers and I tried things like dir() but I could not find more information than that given by line3d? Where am I going wrong? I am quite sure there are arguments controlling background color, the way frames and axes are labeled, etc. How can I find them? -Thanks.ikolTue, 27 Jan 2015 02:39:20 +0100https://ask.sagemath.org/question/25609/Using matrix elements as argumentshttps://ask.sagemath.org/question/7774/using-matrix-elements-as-arguments/I have a rather easy question, or so it would seem. I have looked for an answer but was unable to find one anywhere so I'm asking it here.
I am making a very simple iterative algorithm for which the input as well as the output at the end of every iteration is a vector (or matrix for that matter). What I want to do is use these elements as arguments for several functions during each of the iteration. So for example
x=var('x')
y=var('y')
z=matrix(2,1,[ [1],[1] ]
f=x^2+y^3
H=f.hessian()
Then what I would like to do is say
H(z[0],z[1])
or
H(z)
But no matter what I try I can't seem to get it to work. Ideas?DisneySageFri, 26 Nov 2010 10:44:53 +0100https://ask.sagemath.org/question/7774/List of arguments for a plothttps://ask.sagemath.org/question/10383/list-of-arguments-for-a-plot/Hello.
I'm trying to plot a set of functions with a list of arguments, say
plot(detT, (r,0,7), axes_labels=[r"$r$",r"$\det(T)$"], figsize=4, ticks=[[],[]], fontsize=14)
**Question**
I'd like to define a list or something with all the plot options, say `myplotformat`, and then plot it like
plot(detT, (r,0,7), myplotformat)
- Is it possible?
Cheers.
DoxThu, 25 Jul 2013 16:53:37 +0200https://ask.sagemath.org/question/10383/variables vs argumentshttps://ask.sagemath.org/question/9789/variables-vs-arguments/I'm a newbie so please excuse me with this silly question. I just move from Mathematica to SAGE.
I cannot tell the difference between arguments and variables in SAGE. What're the differences?
Please help.
Thank you in advance! hacaoideasSat, 09 Feb 2013 02:26:22 +0100https://ask.sagemath.org/question/9789/Plotting points with different colourshttps://ask.sagemath.org/question/9701/plotting-points-with-different-colours/I'm trying to plot a number of points such that each has a colour that depends on a certain parameter. I have two lists, `points` and `heights`, and I would like to do something like this:
for i in range(len(points)):
point2d(points[i], rgbcolor = (0,0, heights[i]))
This creates `len(points)` different graphics, so it's not what I want. What do I need to change?manolomartinezFri, 11 Jan 2013 10:58:31 +0100https://ask.sagemath.org/question/9701/Order of arguments of constructors for matrix spaceshttps://ask.sagemath.org/question/8846/order-of-arguments-of-constructors-for-matrix-spaces/Definition: GL(n, R, var=a)
but
Definition: MatrixSpace(base_ring, nrows, ncols=None, sparse=False)
Is there a good reason for the dimension and the ring to be in different order?chaesloc2Wed, 04 Apr 2012 22:28:47 +0200https://ask.sagemath.org/question/8846/a fast function taking either variable or collection as argumenthttps://ask.sagemath.org/question/8536/a-fast-function-taking-either-variable-or-collection-as-argument/I want to create a function which takes either variable or collection as argument.
If the input is any kind of collection, function should be taken of every element in it and return a numpy array of results.
Currently, I do in this way:
def simplefunc(x):
<some code here> # it is assumed that x is a single variable
return <some single result>
def arrayfunc(x):
try: #check if we have a collection
x_iterator = iter(x)
except TypeError: #this is a single expression
return simplefunc(x)
else: # iterate through iterable x
ret=[]
for xx in x:
ret.append(simplefunc(xx))
return numpy.array(ret)
It works as is, however, I do not think this is the fastest way possible,especially the method to figure out if the input is a collection. I also do not like that strings are considered as collections and split to chars (I can stand it though). Is there a more elegant way?
I tried also to call arrayfunc() recursively on each element of collection (instead of simplefunc()), to handle collections of collections in the same way, however, it runs into infinite loop on strings. I have to check if the input is a string explicitly.
ADuC812Wed, 07 Dec 2011 12:58:12 +0100https://ask.sagemath.org/question/8536/How do I get an ordered list of a symbolic functions arguments?https://ask.sagemath.org/question/8462/how-do-i-get-an-ordered-list-of-a-symbolic-functions-arguments/How can I get a list/tuple of the variables in a symbolic function with the same ordering as when the function was defined? e.g. for the function below I would want (z,t) not the alphabetically ordered (t, z) I get with .variables() of .arguments(). The ordering has to be stored/used somewhere in sage because I can differentiate with respect to z and get D[0](u)(z,t) as an answer where the '0' corresponds to 'z'.
sage: var('z,t')
(z, t)
sage: f = function('u',z,t)
sage: print (f)
u(z, t)
sage: f.variables()
(t, z)
sage: f.arguments()
(t, z)
sage: f.diff(z)
D[0](u)(z, t)
sage: f.diff(t)
D[1](u)(z, t)
rtrwalkerThu, 10 Nov 2011 15:41:49 +0100https://ask.sagemath.org/question/8462/graphics_array argumentshttps://ask.sagemath.org/question/7828/graphics_array-arguments/For some reason, not all the arguments for graphics objects are transferred to graphics_array. For example, this code shows the axes and frames even though it is disabled in the individual graphics objects:
<pre>graphics_array([matrix_plot([[0,1,2]], frame=False, axes=False), matrix_plot([[0,1,2]], frame=False, axes=False)])</pre>
Is this a bug, and is there a way around this?Eviatar BachMon, 20 Dec 2010 20:43:28 +0100https://ask.sagemath.org/question/7828/Interact depending on other argumentshttps://ask.sagemath.org/question/7821/interact-depending-on-other-arguments/Hello,
Is it possible to have interact arguments depend on one another? For example, having the maximum of one slider depend on the former? I tried this:
<pre>@interact
def metacontrol(a=slider(5,10)):
@interact
def control(b=slider(0, a)):
pass
</pre>
But it doesn't work; updating the first slider deletes the second slider. It is possible with two cells, but this requires executing the cell which defeats the purpose of interact.
Thank you.Eviatar BachSat, 18 Dec 2010 02:05:41 +0100https://ask.sagemath.org/question/7821/