Ask Your Question

Dmitry Semikin's profile - activity

2019-12-16 18:00:51 +0200 received badge  Popular Question (source)
2019-12-16 18:00:51 +0200 received badge  Notable Question (source)
2015-03-02 19:36:17 +0200 received badge  Great Question (source)
2014-02-09 18:17:31 +0200 received badge  Famous Question (source)
2012-11-01 06:20:14 +0200 received badge  Taxonomist
2012-10-22 16:17:39 +0200 received badge  Notable Question (source)
2012-02-02 02:35:06 +0200 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, so maybe a few more wouldn't be a bad idea.

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


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:, generate_plot_points) WARNING: When plotting,
failed to evaluate function at 39 points.
verbose 0 (4190:, 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.


2012-01-27 09:53:33 +0200 received badge  Popular Question (source)
2011-08-25 07:54:41 +0200 received badge  Good Question (source)
2011-05-12 05:50:28 +0200 received badge  Scholar (source)
2011-05-12 05:50:28 +0200 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-12 05:49:28 +0200 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-12 05:47:22 +0200 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-12 05:31:09 +0200 received badge  Supporter (source)
2011-05-11 20:46:51 +0200 received badge  Nice Question (source)
2011-05-11 16:42:16 +0200 received badge  Student (source)
2011-05-11 15:53:09 +0200 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.