Ask Your Question

Dmitry Semikin's profile - activity

2019-12-16 11:00:51 -0600 received badge  Notable Question (source)
2019-12-16 11:00:51 -0600 received badge  Popular Question (source)
2015-03-02 12:36:17 -0600 received badge  Great Question (source)
2014-02-09 11:17:31 -0600 received badge  Famous Question (source)
2012-11-01 00:20:14 -0600 received badge  Taxonomist
2012-10-22 09:17:39 -0600 received badge  Notable Question (source)
2012-02-01 19:35:06 -0600 marked best answer generate_plot_points fails to evaluate points, while plot imaginary part of expression

While it's true that P1(float(0)) works, P1(float(-1)) doesn't:

sage: P1(float(-1))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
ValueError: negative number cannot be raised to a fractional power

I'm not sure what the best way to avoid this problem would be. Probably we could get away with coercing the function argument to RDF instead of float before it's called-- making that one change in generate_plot_points got the function to plot without difficulty. It's already done in lots of places in plot.py, so maybe a few more wouldn't be a bad idea.

2012-02-01 10:12:58 -0600 received badge  Editor (source)
2012-02-01 10:09:50 -0600 asked a question generate_plot_points fails to evaluate points, while plot imaginary part of expression

Hello,

I solve cubic equation and try to plot imaginary part of its solutions (to find out, at which intervals roots are purely real).

The code is something like this

var('a, b, P, E')

P_sols = solve(a*P + b*P**3 - E, P)

P1 = lambda E_: P_sols[0].rhs().subs({a:-5, b:5, E:E_}).n().imag()
plot(P1, [-10, 10])

Plot function (actually, generate_plot_command) fails to evaluate at points, where image part is nearly zero (range approximately between -2 and 2).

verbose 0 (4190: plot.py, generate_plot_points) WARNING: When plotting,
failed to evaluate function at 39 points.
verbose 0 (4190: plot.py, generate_plot_points) Last error message:
'negative number cannot be raised to a fractional power'

But if I try to evaluate this function directly

P1(0)  # OK
P1(float(0))  # OK

it works OK.

I've found on ask-sage (here), that to create callables it is better to use fast_callable, but I still wonder, why I can call function directly, but plot() function cannot. I've looked into code of generate_plot_points and find there only call line (x, f(x)), so I cannot understand, why it do not work.

I've already solved my practical issue with fast_callable(), but I'm still curious about this behavior.

Thanks.

2012-01-27 02:53:33 -0600 received badge  Popular Question (source)
2011-08-25 00:54:41 -0600 received badge  Good Question (source)
2011-05-11 22:50:28 -0600 received badge  Scholar (source)
2011-05-11 22:50:28 -0600 marked best answer substitute expression instead of formal function symbol

I'm not sure I understand why your example doesn't work, but here is a workaround:

sage: var('a b x')
sage: f = function('foo',x)
sage: g(x) = a*foo(x) + b*foo(x)^2
sage: h = g.diff(x)
sage: bar(x) = a*x + b
sage: h.substitute_function(foo, bar)
2*(a*x + b)*a*b + a^2
2011-05-11 22:49:28 -0600 commented answer substitute expression instead of formal function symbol

One more thing: Is there some ability to create multi-line comments with code snippets?

2011-05-11 22:47:22 -0600 commented answer substitute expression instead of formal function symbol

Thanks for your reply. Your example actually works, and here are two points to be mentioned: 1. if we replace in your example h.substitute_function(foo, bar) by h.subs({foo:bar}) we will get exception (something about coercion). 2. if we replace "f = function('foo', x')" with "foo = function('foo', x)" this will not work (i.e. it will not substitute anything). It seems, that there is some obscurity in what is returned by "function()" function (or may be, more precisely, how "assign" ("=") operator behaves). The line "f = function('foo', x)" makes object "foo" the NewSymbolicFunction, while object "f" - Expression (which I did not met in documentations). And it seems, that effects discussed are derived from this.

2011-05-11 22:31:09 -0600 received badge  Supporter (source)
2011-05-11 13:46:51 -0600 received badge  Nice Question (source)
2011-05-11 09:42:16 -0600 received badge  Student (source)
2011-05-11 08:53:09 -0600 asked a question substitute expression instead of formal function symbol

Hi, everyone.

The question seems to be basic, but I cannot find out any simple answer for it...

So, the case is as follows. Let's assume, we have some formal function symbol (which is intended later to be replaced by actual expression)

x = var('x'); f = function('f', x)

and some function, which uses 'f(x)' in its definition:

a, b = var('a b')
g(x) = a*f(x) + b*f(x)^2

now we would like to compute derivative of 'g(x)' (and possibly make some other manipulations)

 h(x) = diff(g, x)

as a result we have h(x), which includes symbols like 'D[0]f(x)'. This can be answer for some problem in general form. But now we would like to evaluate this expression for particular 'f(x)' (and possibly multiple times with different expressinos), e. g. for 'f(x) = a*x + b'.

It is desirable to have this with something like

answer = h.subs({f:a*x + b})  # do not work :(

but this substitution do not work inside derivatives...

So, the question is: is there some simple solution (like simple subs() function) for the case I've described above? Or may be there is some standard tricks/patterns to solve this issue?

Thanks in advance for any suggestions.