Ask Your Question

StevenClontz's profile - activity

2023-12-24 04:48:46 +0200 received badge  Notable Question (source)
2023-12-24 04:48:46 +0200 received badge  Popular Question (source)
2023-08-01 21:21:31 +0200 received badge  Notable Question (source)
2023-08-01 21:21:31 +0200 received badge  Popular Question (source)
2022-10-13 18:16:58 +0200 received badge  Notable Question (source)
2022-01-18 14:18:57 +0200 received badge  Popular Question (source)
2022-01-07 19:04:32 +0200 asked a question Simplifying/negating propcalc formula

Simplifying/negating propcalc formula I have some code to generate randomized boolean formulas involving AND, OR, and IM

2020-08-04 00:50:19 +0200 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 +0200 received badge  Nice Answer (source)
2020-08-03 03:14:11 +0200 received badge  Nice Question (source)
2020-08-03 03:12:14 +0200 received badge  Teacher (source)
2020-08-03 03:12:14 +0200 received badge  Self-Learner (source)
2020-08-03 02:31:55 +0200 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}')
    obj.save(filename)
    with open(filename, 'rb') as f:
        from base64 import b64encode
        b64 = b64encode(f.read()).decode('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 +0200 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 +0200 received badge  Supporter (source)
2020-08-02 10:06:54 +0200 received badge  Student (source)
2020-08-02 00:01:52 +0200 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: https://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/ring.html (https://doc.sagemath.org/html/en/refe...)

2020-08-01 23:56:00 +0200 received badge  Scholar (source)
2020-08-01 18:48:43 +0200 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 +0200 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 +0200 received badge  Editor (source)
2020-06-03 18:59:28 +0200 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:

\frac{2x+4}{(x^2+1)^2}

But evaluating

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

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 +0200 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?