Ask Your Question

StevenClontz's profile - activity

2020-08-04 00:50:19 +0100 commented question Outputting SVG source of a plot

It's a shame that there's no (optional) keyword argument in Sage ala .save('foo.xml',file_format='svg') that lets you explicitly ask for a desired file format.

2020-08-03 08:33:23 +0100 received badge  Nice Answer (source)
2020-08-03 03:14:11 +0100 received badge  Nice Question (source)
2020-08-03 03:12:14 +0100 received badge  Self-Learner (source)
2020-08-03 03:12:14 +0100 received badge  Teacher (source)
2020-08-03 02:31:55 +0100 answered a question Outputting SVG source of a plot

As an illustration of @slelievre's answer, I wrote a couple functions to read in the SVG (or other graphics) source to convert the result into Base64.

def base64(obj, file_format="svg"):
    Generates Base64 encoding of the graphic in the requested file_format.
    if not isinstance(obj,Graphics):
        raise TypeError("Only graphics may be encoded as base64")
    if file_format not in ["svg", "png"]:
        raise ValueError("Invalid file format")
    filename = tmp_filename(ext=f'.{file_format}')
    with open(filename, 'rb') as f:
        from base64 import b64encode
        b64 = b64encode('utf-8')
    return b64

def data_url(obj, file_format="svg"):
    Generates Data URL representing the graphic in the requested file_format.
    b64 = base64(obj, file_format=file_format)
    if file_format=="svg":
        file_format = "svg+xml"
    return f"data:image/{file_format};base64,{b64}"

f = lambda x, y: sin(x + y) + cos(x + y)
p = plot_slope_field(f, (-3, 3), (-3, 3))
du = data_url(p,file_format="png")
from IPython.core.display import display, HTML
display(HTML(f"<img src='{du}'>"))
print(f"<img src='{du}'>")
2020-08-02 17:36:30 +0100 asked a question Outputting SVG source of a plot

Given a plot, how do I return the source code of its SVG representation? (Not save the SVG to disk.)

E.g. this saves to disk:

plot_slope_field(sin(x+y) + cos(x+y), (x,-3,3), (y,-3,3)).save('foo.svg')

I want something like:

plot_slope_field(sin(x+y) + cos(x+y), (x,-3,3), (y,-3,3)).svg_source()

which returns a string containing the source of the SVG.

2020-08-02 15:43:54 +0100 received badge  Supporter (source)
2020-08-02 10:06:54 +0100 received badge  Student (source)
2020-08-02 00:01:52 +0100 commented answer Symbolic Equation 0=0

Awesome, thank you! The key insight I lacked was how to create a "symbolic zero", and various permutations of Google searches never sent me here: (

2020-08-01 23:56:00 +0100 received badge  Scholar (source)
2020-08-01 18:48:43 +0100 asked a question Symbolic Equation 0=0

What's the "correct" way to create the symbolic equation $0=0$ in Sage?

(In particular, 0==0 returns True, so that's a non-starter.)

2020-06-03 21:08:56 +0100 commented answer Simplifying numerator

Thanks for the reply! I was trying to use the sage standard library to solve this, but you're correct, it'd be pretty easy to write a custom function to do what I need in this narrow case. I'm editing the original question to make this clearer.

2020-06-03 19:00:26 +0100 received badge  Editor (source)
2020-06-03 18:59:28 +0100 asked a question Simplifying numerator

I'm writing a script to create quotient rule exercises, but I cannot coerce the fraction to simplify as desired. In particular, I'd like latex(sol) to look like the following:


But evaluating


always seems to factor the numerator to get

2*(x + 2)/(x^2 + 1)^2

and simplify_full() seems to only expand the denominator, not the numerator:

2*(x + 2)/(x^4 + 2*x^2 + 1)

For clarity, I think the general problem is this (perhaps?) surprising phenomenon: latex(f/g) doesn't seem to respect whether the expression f is factored or not.

2018-12-11 09:03:35 +0100 asked a question Customizing latex output of derivative expressions

I'd like to change the output of

sage: latex(f.diff()-3*f)
-3 \, f\left(x\right) + \frac{\partial}{\partial x}f\left(x\right)

to be closer to

sage: latex(f.diff()-3*f)
f' - 3 f

Is there any way to adjust the presentation of derivatives, and the order in which they appear?