ASKSAGE: Sage Q&A Forum - Latest question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 08 Jan 2020 21:36:39 -0600how to get y component of bezier control points?https://ask.sagemath.org/question/49460/how-to-get-y-component-of-bezier-control-points/ or is there a utility in `sage` that returns the bezier control points of a curve given that curve?
im writing a program that takes a curve in polynomial form and returns those control points, this is the part in question:
def bezierControlPoints(poly):
a = sage.var('a')
b = sage.var('b')
c = sage.var('c')
d = sage.var('d')
t = sage.var('t')
bezier = ((1 - t) ** 3) * a + 3 * ((1 - t) ** 2) * t * \
b + 3 * ((1 - t) * (t ** 2)) * c + (t ** 3) * d
eqn1 = poly.substitute(t=0)
eqn2 = poly.substitute(t=.33)
eqn3 = poly.substitute(t=.66)
eqn4 = poly.substitute(t=1)
cb1 = bezier.substitute(t=0)
cb2 = bezier.substitute(t=.33)
cb3 = bezier.substitute(t=.66)
cb4 = bezier.substitute(t=1)
solns = sage.solve([eqn1 == cb1, eqn2 == cb2, eqn3 == cb3,
eqn4 == cb4], a, b, c, d, solution_dict=True)
return [[s[a].n(30), s[b].n(30), s[c].n(30), s[d].n(30)] for s in solns]
but the control points are coordinates, and this returns single numbers like coefficients. for example
f(x) = x^4 - 10*x^3 + 35*x^2 - 50*x + 24
returns
[24.000000, 3.8069265, -4.2949254, -0.30555556]
[-0.30555556, 0.85185185, 0.85185185, -0.30555556]
[-0.30555556, -1.3680046, -1.2661527, 0.00000000]
so how do i finish processing these portions of the control points?
**background**
the parts of the program i have written that i didnt share take the polynomial of arbitrary order, break it up into simpler subcurves, parameterize those subcurves, and those parameterized subcurves are the arguements of the `bezierControlPoints()`, which is called iteratively depending on the order of the polynomial.
alienfetuseaterWed, 08 Jan 2020 21:36:39 -0600https://ask.sagemath.org/question/49460/Different results with for loop and while loophttps://ask.sagemath.org/question/48767/different-results-with-for-loop-and-while-loop/ While writing a Sage script, I ran into a strange case where code worked correctly with specific constants, but when I looped over them in a `for` loop, it no longer worked. After trying this and that, I found that a `while` loop did not have this problem! As this felt very strange, recording the question here as a possible "gotcha".
Here's the code:
var('z')
from collections import defaultdict
p = defaultdict(dict)
N = 30
for h in range(N): p[0][h] = z**h
def fill(w):
for h in range(N - w):
p[w][h] = w/(w+h)*p[w-1][h+1] + h/(w+h)*(p[w][h-1] if h>=1 else 0)
## This doesn't work:
# for w in range(1, N): fill(w)
## Instead we need the below:
w = 1
while w < N:
fill(w)
w += 1
In short, the problem is that `for w in range(1, N): ...` results in all zero polynomials, while `w = 1; while w < N: ...` does not.
ShreevatsaRFri, 15 Nov 2019 16:35:56 -0600https://ask.sagemath.org/question/48767/loaded file not foundhttps://ask.sagemath.org/question/46157/loaded-file-not-found/I am trying to load a python script into sage notebook, with commands load('file.py'), and attach('file.py').
But I am getting the error: `IOError: did not find file`
The file is there! I am using sage 8.6 on Windows 10.
sage: load('C:\\Program Files (x86)\\MyJournal\\Images\\functions.py')
**UPDATE** Sage loads the file from my home directory.
I seems that Sage just does not have access to Program Files directory.
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
<ipython-input-104-057b25088154> in <module>()
----> 1 load('C:\\Program Files (x86)\\MyJournal\\Images\\functions.py')
/opt/sagemath-8.6/local/lib/python2.7/site-packages/sage/misc/persist.pyx in sage.misc.persist.load (build/cythonized/sage/misc/persist.c:2470)()
134
135 if sage.repl.load.is_loadable_filename(filename):
--> 136 sage.repl.load.load(filename, globals())
137 return
138
/opt/sagemath-8.6/local/lib/python2.7/site-packages/sage/repl/load.py in load(filename, globals, attach)
233 break
234 else:
--> 235 raise IOError('did not find file %r to load or attach' % filename)
236
237 ext = os.path.splitext(fpath)[1].lower()
IOError: did not find file 'C:\\Program Files (x86)\\MyJournal\\Images\\functions.py' to load or attachMathF13Sun, 14 Apr 2019 07:16:06 -0500https://ask.sagemath.org/question/46157/Using Sage in a Python CGI scripthttps://ask.sagemath.org/question/8215/using-sage-in-a-python-cgi-script/I am writing cgi scripts in python that use sage. I have been researching for weeks and even the python and sage websites say use: "from sage.all import *" in your python script, and have "#!/usr/bin/env sage -python" as the first line of the script.
Here is the exact quote from sagemath faq on what to do:
"
The following standalone Sage script factors integers, polynomials, etc:
#!/usr/bin/env sage -python
import sys
from sage.all import *
if len(sys.argv) != 2:
print "Usage: %s <n>"%sys.argv[0]
print "Outputs the prime factorization of n."
sys.exit(1)
print factor(sage_eval(sys.argv[1]))
In order to use this script, your SAGE_ROOT must be in your PATH."
I made the cgi file exactly as is, and put onto my web server (Apache running CentOS), with sage and python installed. When I try to run this file (named factor.cgi) through the bash terminal I do:
$ ./factor.cgi 2006
/usr/bin/env: sage -python: No such file or directory
Any help on what I should do to make this script work would be appreciated.randomness431Thu, 07 Jul 2011 10:19:00 -0500https://ask.sagemath.org/question/8215/running sage from a Python scripthttps://ask.sagemath.org/question/9733/running-sage-from-a-python-script/I'm using sage 5.5 on ubuntu 12.04. I'm trying to run sage via the following script
#!/usr/bin/env sage -python
import sys
from sage.all import *
sigma = 10
z = var('z')
m0 = 128
P = exp(-(1/2)*z**2/sigma**2)/(sqrt(2*pi)*sigma)
m = m0*exp(-(1/2)*z**2)
mb = m0/sqrt(1+sigma**2)
f = lambda u: 1-2*(numerical_integral(u*P*m**2/(mb*(u*mb+(1-u)*m)), 0, Infinity))[0]
print find_root(f, 0.1, 1, rtol=0.01)
I run this from the command line with
> sage -python runDispK1.py
and I get the following error
Warning: invalid value encountered in absolute
Warning: invalid value encountered in absolute
Warning: invalid value encountered in absolute
Warning: invalid value encountered in absolute
Warning: invalid value encountered in absolute
Traceback (most recent call last):
File "runDispK1.py", line 16, in <module>
print find_root(f, 0.1, 1, rtol=0.01)
File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/numerical/optimize.py", line 108, in find_root
raise RuntimeError, "f appears to have no zero on the interval"
RuntimeError: f appears to have no zero on the interval
Whereas if I run the exact same code within sage, e.g. I have a file called run.sage with this text in it:
sigma = 10
z = var('z')
m0 = 128
P = exp(-(1/2)*z**2/sigma**2)/(sqrt(2*pi)*sigma)
m = m0*exp(-(1/2)*z**2)
mb = m0/sqrt(1+sigma**2)
f = lambda u: 1-2*(numerical_integral(u*P*m**2/(mb*(u*mb+(1-u)*m)), 0, Infinity))[0]
print find_root(f, 0.1, 1, rtol=0.01)
then sage gives me the correct answer
sage: load("run.sage")
0.550279773862
Is there something I'm missing here? I'm new to sage and python scripting but it seems the numerical_integral function is returning nothing useful, so I are these functions created incorrectly?
Thanks
colin_tTue, 22 Jan 2013 04:36:49 -0600https://ask.sagemath.org/question/9733/How can I re-code a function into a method?https://ask.sagemath.org/question/8482/how-can-i-re-code-a-function-into-a-method/I have the following simple function 'add4' stored in a file 'foo.sage':
def add4(x):
"""Adds 4 to x"""
return x + 4
I can use this in sage by:
sage: attach 'foo.sage'
sage: add4?
Type: function
Base Class: <type 'function'>
String Form: <function add4 at 0x4f332a8>
Namespace: Interactive
File: Dynamically generated function. No source code available.
Definition: add4(x)
Docstring:
Adds 4 to x
sage: var('x')
x
sage: add4(x)
x + 4
How can I change my function to a method so that I can call it using the x.add4() syntax? I still want the method to reside in an external file.
rtrwalkerWed, 16 Nov 2011 11:57:28 -0600https://ask.sagemath.org/question/8482/