Ask Your Question

Laurent Claessens's profile - activity

2020-02-21 20:52:14 -0500 received badge  Notable Question (source)
2020-01-06 16:39:38 -0500 received badge  Notable Question (source)
2019-10-13 11:41:50 -0500 received badge  Famous Question (source)
2019-05-02 02:05:51 -0500 received badge  Notable Question (source)
2019-04-12 06:52:16 -0500 received badge  Notable Question (source)
2017-02-15 23:20:15 -0500 received badge  Famous Question (source)
2016-08-28 16:22:21 -0500 received badge  Popular Question (source)
2016-04-18 03:14:45 -0500 received badge  Popular Question (source)
2016-01-17 11:17:32 -0500 received badge  Taxonomist
2015-10-29 04:23:06 -0500 received badge  Notable Question (source)
2015-06-05 07:56:03 -0500 received badge  Popular Question (source)
2015-01-05 18:54:02 -0500 received badge  Famous Question (source)
2014-07-21 12:26:36 -0500 received badge  Popular Question (source)
2014-06-28 20:14:49 -0500 marked best answer ValueError: free variable x |--> x when plotting the function x

Hi all.

I got the following problem:

sage: plot(symbolic_expression(x).function(x))

this raises

ValueError: free variable: x |--> x

If I replace x by anything else (but 1*x) it works fine.

How can I do ?

My rationale behind my question is that I have a class which takes a function as argument and can perform many thinks on it, among other the plot. I made the following :

class MyFunction(object):
    def __init__(self,f):
    def plot(self):
        return plot(self.f)

My point in doing so is that I have to accept, as input f expressions like x**2, 2, g.diff(x) (where g is an other function) and so on. In these cases, it turns out that I need to use the symbolic_expression trick in order to be sure that what I have is a function (need for numerical integration for example)

My questions :

  1. Can I do otherwise in __init__ in order to be sure to be able to use numerical integration, derivative, ... on self.f ?

  2. If not, how can I plot when the input is simply "x" ?

2014-06-28 20:14:47 -0500 marked best answer get_minmax_data on implicit_plot

This is a sequel of my question about plotting level set.

In the following, G is a circle :

sage: f(x,y)=x**2+y**2  
sage: G=implicit_plot(f==1,(x,-2,2),(y,-3,3))
sage: G.get_minmax_data()
{'xmin': -2.0, 'ymin': -3.0, 'ymax': 3.0, 'xmax': 2.0}

The "correct" get_minmax_data sould be

{'xmin': -1.0, 'ymin': -1.0, 'ymax': 1.0, 'xmax': 1.0}

As far as I understood the code (and the thread "Retrieving xy data from implicit plots" on Sage-support), the following is the relevant part :

xy_data_arrays = numpy.asarray([[[func(x, y) for x in xsrange(*ranges[0],include_endpoint=True)]
                                 for y in xsrange(*ranges[1], include_endpoint=True)]
                                for func in g],dtype=float)

in ../plot/

My questions are :

  1. can I retrieve that xy_data_array ?

  2. If I analyse xy_data_array, I suppose that extracting the point with lowest x-component such that the value is positive will provide me the "correct" xmin of the plot. I'm wrong ?

2014-06-28 20:14:47 -0500 marked best answer Plot the level sets of a function

I'm trying to draw level set of a function f:R^2->R, that is the set of solutions of f(x,y)=h for a given h.

For that purpose I wrote the following

#! /usr/bin/sage -python
# -*- coding: utf8 -*-

from sage.all import *

def level_curve(f,h):
    solutions_list = solve(f==h,y)
return [sol.rhs() for sol in solutions_list]

for g in level_curve(f,3):
print g

print "-----"

for g in level_curve(f,3):
    print g

This works, but I'm not satisfied principally because I got the level sets under the form of a list of functions. Moreover it will not work if the level set is vertical.

Thus I would prefer to get the solution under the form of a parametric curve.

Does Sage provides something for that ?

2014-05-08 02:59:26 -0500 received badge  Notable Question (source)
2014-01-07 18:50:04 -0500 received badge  Notable Question (source)
2013-12-19 22:43:02 -0500 commented answer Solve equation 1/3*x + sin(2*x)==1

Thanks for your answer, tmonteil. That solves the equation with enough accuracy for my purpose but it does not solves my full problem because I have a system. Ultimately I would like to know the intersection points of two curves. In my example the second curve was too easy : y-1=0. Since many painting softwares are able to fill the region between two curves (e.g. pstricks), I guess this is possible ...

2013-12-14 20:26:48 -0500 asked a question Solve equation 1/3*x + sin(2*x)==1

Hi all.

I have the equations

y - 1 == 0,
y == 1/3*x + sin(2*x)

and I want solutions. I know by the intermediate value theorem that there are two solutions : about x=0.5 and x=1.25. I'd like Sage to give me these solutions. I already tried to_poly_solve=True and/or explicit_solutions=True.

As an example of failure :

sage: solve(  1/3*x + sin(2*x)==1,x,explicit_solutions=True  )

What can I do ?

Thanks Laurent Claessens

2013-08-28 22:02:18 -0500 received badge  Popular Question (source)
2013-06-26 11:15:50 -0500 received badge  Popular Question (source)
2013-03-18 21:54:30 -0500 received badge  Popular Question (source)
2013-03-12 02:07:38 -0500 received badge  Notable Question (source)
2013-01-27 12:31:08 -0500 received badge  Notable Question (source)
2012-12-14 04:13:17 -0500 received badge  Popular Question (source)
2012-09-29 04:09:05 -0500 asked a question PolynomialRing and from __future__ import unicode_literals


sage: from __future__ import unicode_literals
sage: R=PolynomialRing(QQ,'x')

TypeError Traceback (most recent call last)

/home/moky/script/<ipython console=""> in <module>()

/home/moky/Sage/local/lib/python2.7/site-packages/sage/rings/polynomial/polynomial_ring_constructor.pyc in PolynomialRing(base_ring, arg1, arg2, sparse, order, names, name, implementation) 425 if R is None: 426 raise TypeError("invalid input (%s, %s, %s) to PolynomialRing function; please see the docstring for that function"%( --> 427 base_ring, arg1, arg2)) 428 429 return R

TypeError: invalid input (Rational Field, x, None) to PolynomialRing function; please see the docstring for that function

I guess this is the same kind of problem that the one in this question

By the way, this is "fixed" by using str :

sage: R=PolynomialRing(QQ,str('x'))
sage: f=R.lagrange_polynomial([(0,1),(1,4)]);f
3*x + 1
2012-08-05 12:10:46 -0500 received badge  Great Answer (source)
2012-05-20 10:47:09 -0500 received badge  Nice Answer (source)
2012-05-20 02:55:23 -0500 answered a question convert expression to function

Does it answer the question ?

sage: s
sage: f=s.function(x)
sage: f
x |--> sin(x)
2012-05-02 00:41:45 -0500 received badge  Popular Question (source)
2012-04-08 05:20:14 -0500 commented question get_minmax_data too generosous ?

By the way, what is the correct way to know the global xmin,xmax,ymin and ymax of a function ? A 3-digit approximation is enough. Up to now I'm using get_minmax_data, but is it the correct way ?

2012-04-08 04:32:35 -0500 asked a question get_minmax_data too generosous ?


I'd like to understand the rationale behind the ymin value in the get_minmax_data of a parametric curve that have always y=0:

sage: f(x)=sin(x)
sage: g(x)=0
sage: P=parametric_plot((f,g),(-pi/2,2*pi))
sage: P.get_minmax_data()['ymin']

I would have expected ymin to be 0. Is is a bug or is it something I don't understand ?

2012-03-21 21:53:15 -0500 received badge  Commentator
2012-03-21 21:53:15 -0500 commented answer min(x,y)=x ... and then plot3d f(x,y)=min(x,y)

Yes, `min_symbolic` is the answer. Thanks :)

2012-03-21 21:52:24 -0500 marked best answer min(x,y)=x ... and then plot3d f(x,y)=min(x,y)

I can't find this, but I'm pretty sure there is another question with this on

sage: var('y')
sage: min(x,y)
sage: min_symbolic(x,y)
min(x, y)

I think if you use the latter, all should be well. I hope? Don't have time to try now. Good luck!

2012-03-21 10:05:43 -0500 asked a question min(x,y)=x ... and then plot3d f(x,y)=min(x,y)

Hi all

I can understand the following somewhat surprising result :

sage: var('x,y')
(x, y)
sage: min(x,y)

A friend of mine wants to plot3d the function that a human should write f(x,y)=min(|x|,|y|)

what she does is

sage: f(x,y)=min(abs(x),abs(y))
sage: plot3d(f,(x,-2,2),(y,-2,2))

of course, it does not produce the expected result because of what I said in introduction about min(x,y). By the way :

sage: f(4,1)  

So ... well ... what do I have to say to her ? What is the best way to plot a function (in the math sense of the term) when it cannot be managed by a function (in the Sage sense of the term).

The following works :

sage: f=lambda x,y:min(abs(x),abs(y))

I guess that

def f(x,y):
   return min(abs(x),abs(y))

will also work.

So my questions are :

  1. why min(x,y)=x ?
  2. how can I "predict" if such or such function will not work using the simple declaration f(x,y)=blahblah ?
  3. what is the best way to deal with such cases ?

Thanks for any help

have a good night

Laurent Claessens (on the night timezone :) )

2012-03-02 22:00:28 -0500 answered a question Type error in recursion

Your error is not due to recursion. The following example (slightly simplified with respect to yours) produces the same error :

def f(n) :
    def retfunc(x) : 
        if n==0:
            return 1
        return x*f(3)
    return retfunc


The error is at x*f(3). f(3) is a function, not a number. I don't really understand what you are trying to do. Some kind of factorial ?

The following code does not produce the error :

def f(n) :
    def retfunc(x) : 
        if n==0:
            return 1
        return x*f(n-1)(3)
    return retfunc

print w(3)

Note : f(n-1)(3)

Hope it helps


2012-02-23 14:41:54 -0500 received badge  Popular Question (source)
2012-02-02 00:58:11 -0500 asked a question Sage for (very) undergrad students

Hi all

Today I had hard time with Sage because

sage: f(x)=1/(1-x**2)
sage: g(x)=f.integrate(x)
sage: g(0.5)
0.549306144334055 - 1.57079632679490*I
sage: g
x |--> -1/2*log(x - 1) + 1/2*log(x + 1)

This is not the primitive my students are expecting : they expect 1-x on the denominator. In particular they are not expecting :

sage: ln(-1)                                   

I had other some small problems like that with Sage like the difference between +Inf,-Inf and Inf when computing a limit (in the latter case my students are expecting "does not exist").

So my question is : is there a way to ask Sage to behave like a very undergrad student is expected to behave ? (ex : ln(-1) does not exist) Is there a way to say «I'm an undergrad student and I want Sage to solve my homework of basics calculus» ?

Do you have experience/habits to prevent Sage to mislead students by its unexpected answers (however mathematically correct) ?

2012-01-26 18:25:54 -0500 answered a question solve and calculate

A variation on the Shashank'answer :

sage: z=var('z')
sage: f(z)=solve(x+z^2==1,x)[0].rhs()
sage: f
z |--> -z^2 + 1
sage: f.diff(z)
z |--> -2*z
sage: f.plot()

Here you consider the function "solution of the equation as function of z", and you do whatever with it.