Ask Your Question

daniel.e2718's profile - activity

2020-11-10 20:59:57 +0200 received badge  Nice Question (source)
2020-11-06 18:25:28 +0200 received badge  Good Answer (source)
2019-06-12 13:55:19 +0200 received badge  Popular Question (source)
2019-04-25 18:51:17 +0200 received badge  Nice Answer (source)
2019-03-14 17:37:47 +0200 received badge  Good Question (source)
2019-02-24 20:45:19 +0200 received badge  Famous Question (source)
2019-02-24 20:45:19 +0200 received badge  Notable Question (source)
2018-01-23 17:16:37 +0200 received badge  Famous Question (source)
2017-09-17 23:12:25 +0200 received badge  Popular Question (source)
2016-02-06 19:50:43 +0200 received badge  Notable Question (source)
2015-08-27 00:10:47 +0200 received badge  Famous Question (source)
2015-08-25 12:29:42 +0200 received badge  Famous Question (source)
2014-11-13 19:08:19 +0200 received badge  Notable Question (source)
2014-06-29 03:15:17 +0200 marked best answer Save 3d plot as vector format

Is there a way to export a 3d plot as a pdf file?

I know you can't do'filename.extension') because 3dplot doesn't create a graphics item. But is there a way to do this?

2014-06-29 03:15:16 +0200 marked best answer creating a group of similar functions with similar names
namelist = ['f'+str(i) for i in range(10)]; namelist
for i in range(len(namelist)):
    namelist[i] = lambda x: 1/x^i

1 - create a list of function names (f0, f1, f2, ...)

2 - for each item in namelist

3 - take that that item and make it a function that raises x to a negative power equal to the position of that item (f0(x) = 1/x^0, f1(x) = 1/x^1, ...)

Is this possible?


Made some progress by changing the type of namelist[i] to Expression by var(i)...

2014-06-29 03:15:16 +0200 marked best answer plotting multiple functions from a for loop

Here I link to a worksheet with which I'm having the darnedest trouble...


The code in the worksheet makes it easy to plot many similar plots, and this is a pretty organized way to do it. I got it to work once, but I changed something and made too many changes afterward to get it working again to undo back to my original code.

The problem is in the third cell, obviously. I want to assign to 'a' "plot all of the functions in the list funclist." I realize that they aren't functions, really (they aren't assigned names such as f0,f1,f2, etc), but I'll try to solve that another day. But anyway, how can I set this loop up to add all of these plots and dump them into an object? I need to be able to input 'a' and get a plot containing all of these.

As is evident from the worksheet, I can't use the operator += (though I swear I did the first time).

I would rather not to do it like this:

a = plotlist[0]
for i in range(1,len(plotlist)):
    a += plotlist[i]

Because that requires that I spread out the definition of 'a' and has an unnecessary amount of code. However, that seems to be the easiest way to do it.

But I would like to be able to do it in two lines of code --- for i in plotlist, add all these plots to this object. Or three lines, if I have to initialize 'a' beforehand (give it a type or whatever.)

2014-06-29 03:15:15 +0200 marked best answer use output of solve() without 'var == '

Is there a way to use the values of solve() without having that pesky 'var == ' in front of it?

in   h = x^2-9; h
out     x |--> x^2 - 9
in   sols = solve(h == 0, x); sols
out     [x == -3, x == 3]
in   sols[0]
out     x == -3
in   h(sols[0])
out     (x == -3)^2 - 9

What the heck is that last line?!

Is there a way to extract the value of sols[0] for use without needing to copy/paste the value?

2014-04-09 07:24:36 +0200 received badge  Popular Question (source)
2014-01-04 21:28:19 +0200 received badge  Nice Question (source)
2013-12-11 16:05:34 +0200 received badge  Popular Question (source)
2013-12-05 08:52:13 +0200 received badge  Notable Question (source)
2013-11-22 07:38:56 +0200 received badge  Famous Question (source)
2013-09-22 13:42:55 +0200 received badge  Popular Question (source)
2013-09-13 16:51:25 +0200 marked best answer Save 3d plot as vector format

The Sage 3D plots don't support output in a 2D vector format (like PDF), at least not for now. You can do it by calling matplotlib directly: PDF version of the image below

image description

from mpl_toolkits.mplot3d import axes3d
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import rc
rc('text', usetex=True)

rays = [(-3, -2, 4), (0, 1, 0), (1, 0, 0), (2, 1, -4)]
extrapoints = [(-1, -1, 2), (1, 1, -2)]
cones = [[0,1,2],[0,1,3],[0,2,3],[1,2,3]]

poly3d = []
for cone in cones:
    verts = [ rays[i] for i in cone ]

polygons = Poly3DCollection(poly3d, facecolor='green', 

fig = plt.figure()

ax = axes3d.Axes3D(fig)
ax.scatter3D(*zip(*rays), s=50, c='red')
ax.scatter3D(*zip(*extrapoints), s=50, c='blue')
ax.scatter3D([0],[0],[0], s=20, c='black')

ax.set_xlim3d(-3, 2)
ax.set_ylim3d(-2, 1)
ax.set_zlim3d(-4, 4)

r = rays[0]
ax.text(r[0]-0.2,r[1],r[2], r'${}^{'+str(r)+'}$', 

r = rays[3]
ax.text(r[0]-0.2,r[1],r[2], r'${}^{'+str(r)+'}$', 

r = rays[1]
ax.text(r[0],r[1],r[2]+0.3, r'${}^{'+str(r)+'}$', 

r = rays[2]
ax.text(r[0],r[1],r[2]-0.3, r'${}^{'+str(r)+'}$', 

r = extrapoints[0]
ax.text(r[0],r[1],r[2]-0.3, r'${}^{'+str(r)+'}$', 

r = extrapoints[1]
ax.text(r[0]+0.2,r[1],r[2], r'${}^{'+str(r)+'}$', 

ax.text(-0.2,0,0, r'${}^0$', 


for axis in [ax.w_xaxis, ax.w_yaxis, ax.w_zaxis]:
    axis.get_major_locator()._integer = True
    for t in axis.get_ticklabels(): 

plt.savefig('fig_NablaBsing.pdf', bbox_inches='tight')
2013-07-19 05:10:14 +0200 received badge  Taxonomist
2013-04-30 07:07:02 +0200 received badge  Popular Question (source)
2013-03-28 12:08:10 +0200 received badge  Self-Learner (source)
2013-03-28 12:08:10 +0200 received badge  Teacher (source)
2013-02-21 11:32:49 +0200 received badge  Notable Question (source)
2012-10-23 16:19:47 +0200 asked a question Map a function to each element of a matrix?

Is there a direct way to apply a function to each entry of a matrix and return the resulting matrix?

A = matrix([[1,2],[3,4]])
B = matrix([[j^2 for j in i] for i in A])

Not terribly hard, but it's not ideal.

2012-10-22 17:26:34 +0200 commented answer How do I make functional substitutions?

*Exactly* what I was looking for! Thanks! EDIT: Good question though. Why doesn't it work if you specify the variable of the function?

2012-10-22 17:26:04 +0200 marked best answer How do I make functional substitutions?

I think you want v = function('v') for the first line; the first argument is the name of the function.

Here's the full code:

v = function('v')
u(x) = (v(x) + v(-x))/2
delta = 10^(-90)
u(delta).substitute_function(v, gamma)

I'm puzzled as to why it doesn't work if v is defined as v = function('v', x). Anyone know?

2012-10-22 16:16:35 +0200 asked a question How do I make functional substitutions?

I want to substitute one function for another. Consider this:

v = function('x')
u(x) = (v(x)+v(-x))/2
delta = 10^(-90)

The very last line is what I'm trying to accomplish. I know it's the wrong command, but I'm wanting to make the substitution $v(x)\to\Gamma(x)$.

Is there a way along these lines to do such a functional substitution?

2012-10-14 14:19:07 +0200 received badge  Popular Question (source)
2012-09-30 11:59:14 +0200 marked best answer plotting multiple functions from a for loop

I think you're going to be interested in list comprehensions and generator expressions.

Using listcomps, your code compresses to:

sage: funclist = [1/x^i for i in range(10)]
sage: plotlist = [f.plot((x, 1, 10)) for f in funclist]
sage: plot(plotlist)

(Note that I changed the x lower limit to make it look better.)

image description

[BTW, as for your ability to use "a += something", that was almost certainly because you already had an "a" floating around from before.]

If you want to one-line it:

plot([(1/x^i).plot((x, 1, 10)) for i in range(10)])
2012-06-23 23:57:02 +0200 commented answer Sagetex and windows 7?

Done. (insert extra characters here to reach 10 character minimum)