Ask Your Question

Laurent Claessens's profile - activity

2023-03-03 19:52:52 +0200 received badge  Famous Question (source)
2022-06-14 15:09:12 +0200 received badge  Famous Question (source)
2022-01-16 02:33:51 +0200 received badge  Notable Question (source)
2021-08-05 08:47:36 +0200 received badge  Notable Question (source)
2021-08-05 08:47:36 +0200 received badge  Popular Question (source)
2021-04-12 18:32:27 +0200 received badge  Famous Question (source)
2020-02-22 03:52:14 +0200 received badge  Notable Question (source)
2020-01-06 23:39:38 +0200 received badge  Notable Question (source)
2019-10-13 18:41:50 +0200 received badge  Famous Question (source)
2019-05-02 09:05:51 +0200 received badge  Notable Question (source)
2019-04-12 13:52:16 +0200 received badge  Notable Question (source)
2017-02-16 06:20:15 +0200 received badge  Famous Question (source)
2016-08-28 23:22:21 +0200 received badge  Popular Question (source)
2016-04-18 10:14:45 +0200 received badge  Popular Question (source)
2016-01-17 18:17:32 +0200 received badge  Taxonomist
2015-10-29 10:23:06 +0200 received badge  Notable Question (source)
2015-06-05 14:56:03 +0200 received badge  Popular Question (source)
2015-01-06 01:54:02 +0200 received badge  Famous Question (source)
2014-07-21 19:26:36 +0200 received badge  Popular Question (source)
2014-06-29 03:15:08 +0200 marked best answer 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)
x

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)  
4

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 :) )

2014-06-29 03:14:49 +0200 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):
        self.f=symbolic_expression(f).function(x)
    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-29 03:14:47 +0200 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/contour_plot.py

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-29 03:14:47 +0200 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]

var('x,y')
f=x+y+2
for g in level_curve(f,3):
print g

print "-----"

f=x**2+y**2
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 09:59:26 +0200 received badge  Notable Question (source)
2014-01-08 01:50:04 +0200 received badge  Notable Question (source)
2013-12-20 05:43:02 +0200 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-15 03:26:48 +0200 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-29 05:02:18 +0200 received badge  Popular Question (source)
2013-06-26 18:15:50 +0200 received badge  Popular Question (source)
2013-03-19 03:54:30 +0200 received badge  Popular Question (source)
2013-03-12 08:07:38 +0200 received badge  Notable Question (source)
2013-01-27 19:31:08 +0200 received badge  Notable Question (source)
2012-12-14 11:13:17 +0200 received badge  Popular Question (source)
2012-09-29 11:09:05 +0200 asked a question PolynomialRing and from __future__ import unicode_literals

Hello

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 19:10:46 +0200 received badge  Great Answer (source)
2012-05-20 17:47:09 +0200 received badge  Nice Answer (source)
2012-05-20 09:55:23 +0200 answered a question convert expression to function

Does it answer the question ?

sage:s=sin(x)                                                                  
sage: s
sin(x)
sage: f=s.function(x)
sage: f
x |--> sin(x)
2012-05-02 07:41:45 +0200 received badge  Popular Question (source)
2012-04-08 12:20:14 +0200 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 11:32:35 +0200 asked a question get_minmax_data too generosous ?

Hello

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']
-1.0

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

2012-03-22 03:53:15 +0200 commented answer min(x,y)=x ... and then plot3d f(x,y)=min(x,y)

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

2012-03-22 03:53:15 +0200 received badge  Commentator
2012-03-22 03:52:28 +0200 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 ask.sagemath.org.

sage: var('y')
y
sage: min(x,y)
x
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!