Ask Your Question

Revision history [back]

When you input f(x,a) into numerical_integral, it already knows that a is a parameter. The optional input params is a way for you to give the value of the parameters.

sage: var('x a')
sage: f(x,a)=a*x
sage: numerical_integral(f(x,a),0,1, params=[6])
(2.9999999999999996, 3.330669073875469e-14)

I'm not sure exactly what happens when you evaluate

sage: numerical_integral(f(x,a),0,1, params=[a])
(0.3333333333333333, 3.700743415417188e-15)

--- it would seem that the parameter disappears (or is given the value 1). This means your g(x,a) defined as

sage: g(x,a)=numerical_integral(f(x,a),0,1, params=[a])

doesn't depend on a, so g(x,anything) will always get you the same value.

For some reason, it doesn't this way:

sage: def g(x,a):
....:     return numerical_integral(f(x,a),0,1, params=[a])
....: 
sage: g(x,6)
(2.9999999999999996, 3.330669073875469e-14)

There's still something to figure out here!

A few observations.

  1. A note. When you feed numerical_integral with the input f(x,a) into numerical_integral, it already detects that the input has two variables and knows that a one of them is a parameter. parameter. The optional input params is a way for you to lets one give the valuevalues of the parameters.

    sage: var('x a') sage: f(x,a)=a*x sage: numerical_integral(f(x,a),0,1, params=[1]) (0.5, 5.551115123125783e-15) sage: numerical_integral(f(x,a),0,1, params=[6]) (2.9999999999999996, 3.330669073875469e-14)

    I'm 3.330669073875469e-14)

  2. What is not sure exactly what happens when clear is which of the two variables in f is considered by numerical_integral as being the parameter. Is it x or a? Which is the main variable, the first one or the last one, or can it be specified?

  3. A surprise. When you evaluate

    sage: numerical_integral(f(x,a),0,1, params=[a]) (0.3333333333333333, 3.700743415417188e-15)

it seems that the parameter is transformed into the main variable before integrating: the answer is exactly the integral of $x^2$ (or $a^2$ if the integration is with respect to $a$) from 0 to 1.

sage: numerical_integral(x^2,0,1,)
(0.3333333333333333, 3.700743415417188e-15)

--- Not sure if this behaviour is wanted or a bug.

  1. Weirder (if you thought a was the parameter and x the main variable):

    sage: f(x,a)=a*x^2 sage: numerical_integral(f(x,a),0,1, params=[1]) (0.5, 5.551115123125783e-15) sage: numerical_integral(f(x,a),0,1, params=[a]) (0.25, 2.7755575615628914e-15)

Now the integral with parameter set to 1 is wrong, unless integration is with respect to $a$ rather than $x$. With params=[a] it would seem that the parameter disappears (or is given the value 1). This means your g(x,a) defined as

sage: g(x,a)=numerical_integral(f(x,a),0,1, params=[a])

doesn't now looks like x was set to a before integrating with respect to a.

  1. Since numerical_integral(f(x,a),0,1, params=[a]) has a fixed value, when you define

    sage: g(x,a) = numerical_integral(f(x,a),0,1, params=[a])

this does not depend on x or a, so g(x,anything)g(anything,anything) will always get you always return the same value.

  1. For some reason, it the "same" g defined using def doesn't this way:

    behave the same:

    sage: def g(x,a): ....: return numerical_integral(f(x,a),0,1, params=[a]) ....: sage: g(x,6) (2.9999999999999996, 3.330669073875469e-14)

    There's still something to figure out here!

    3.330669073875469e-14)

A few observations.

  1. A note. When you feed numerical_integral with the input f(x,a), it detects that the input has two variables and knows one of them is a parameter. The optional input params lets one give the values of the parameters.

    sage: var('x a')
    sage: f(x,a)=a*x
    sage: numerical_integral(f(x,a),0,1, params=[1])
    (0.5, 5.551115123125783e-15)
    sage: numerical_integral(f(x,a),0,1, params=[6])
    (2.9999999999999996, 3.330669073875469e-14)

    3.330669073875469e-14)
  2. What is not clear is which of the two variables in f is considered by numerical_integral as being the parameter. Is it x or a? Which is the main variable, the first one or the last one, or can it be specified?

  3. A surprise. When you evaluate

    sage: numerical_integral(f(x,a),0,1, params=[a])
    (0.3333333333333333, 3.700743415417188e-15)

3.700743415417188e-15)

it seems that the parameter is transformed into the main variable before integrating: the answer is exactly the integral of $x^2$ (or $a^2$ if the integration is with respect to $a$) from 0 to 1.

sage: numerical_integral(x^2,0,1,)
(0.3333333333333333, 3.700743415417188e-15)

Not sure if this behaviour is wanted or a bug.

  1. Weirder (if you thought a was the parameter and x the main variable):

    sage: f(x,a)=a*x^2
    sage: numerical_integral(f(x,a),0,1, params=[1])
    (0.5, 5.551115123125783e-15)
    sage: numerical_integral(f(x,a),0,1, params=[a])
    (0.25, 2.7755575615628914e-15)

2.7755575615628914e-15)

Now the integral with parameter set to 1 is wrong, unless integration is with respect to $a$ rather than $x$. With params=[a] it now looks like x was set to a before integrating with respect to a.

  1. Since numerical_integral(f(x,a),0,1, params=[a]) has a fixed value, when you define

    sage: g(x,a) = numerical_integral(f(x,a),0,1, params=[a])

params=[a])

this does not depend on x or a, so g(anything,anything) will always return the same value.

  1. For some reason, the "same" g defined using def doesn't behave the same:

    sage: def g(x,a):
    ....:     return numerical_integral(f(x,a),0,1, params=[a])
    ....: 
    sage: g(x,6)
    (2.9999999999999996, 3.330669073875469e-14)

    3.330669073875469e-14)

A few observations.

  1. A note. When you feed numerical_integral with the input f(x,a), it detects that the input has two variables and knows one of them is a parameter. The optional input params lets one give the values of the parameters.

    sage: var('x a')
    sage: f(x,a)=a*x
    sage: numerical_integral(f(x,a),0,1, params=[1])
    (0.5, 5.551115123125783e-15)
    sage: numerical_integral(f(x,a),0,1, params=[6])
    (2.9999999999999996, 3.330669073875469e-14)
    
  2. What is not clear is which of the two variables in f is considered by numerical_integral as being the parameter. Is it x or a? Which is the main variable, the first one or the last one, or can it be specified?

  3. A surprise. When you evaluate

    sage: numerical_integral(f(x,a),0,1, params=[a])
    (0.3333333333333333, 3.700743415417188e-15)
    

    it seems that the parameter is transformed into the main variable before integrating: the answer is exactly the integral of $x^2$ (or $a^2$ if the integration is with respect to $a$) from 0 to 1.

    sage: numerical_integral(x^2,0,1,)
    (0.3333333333333333, 3.700743415417188e-15)
    

    Not sure if this behaviour is wanted or a bug.

  4. Weirder (if you thought a was the parameter and x the main variable):

    sage: f(x,a)=a*x^2
    sage: numerical_integral(f(x,a),0,1, params=[1])
    (0.5, 5.551115123125783e-15)
    sage: numerical_integral(f(x,a),0,1, params=[a])
    (0.25, 2.7755575615628914e-15)
    

    Now the integral with parameter set to 1 is wrong, unless integration is with respect to $a$ rather than $x$. With params=[a] it now looks like x was set to a before integrating with respect to a.

  5. Since numerical_integral(f(x,a),0,1, params=[a]) has a fixed value, when you define

    sage: g(x,a) = numerical_integral(f(x,a),0,1, params=[a])
    

    this does not depend on x or a, so g(anything,anything) will always return the same value.

  6. For some reason, the "same" g defined using def doesn't behave the same:

    sage: def g(x,a):
    ....:     return numerical_integral(f(x,a),0,1, params=[a])
    ....: 
    sage: g(x,6)
    (2.9999999999999996, 3.330669073875469e-14)
    

    Here, g(x,6) returns numerical_integral(f(x,6),0,1, params=[6]), in which the function to integrate only has one variable, x, and the optional parameter params=[6] is just ignored.

    You could simply write:

    sage: def g(a):
    ....:     return numerical_integral(f(x,a),0,1)
    ....: 
    sage: g(6)
    (2.9999999999999996, 3.330669073875469e-14)
    

A few observations.observations can help explain what you are experiencing.

  1. A note. When you feed numerical_integral with the input f(x,a), it detects that the input has two variables and knows one of them is a parameter. The optional input params lets one give the values of the parameters.

    sage: var('x a')
    sage: f(x,a)=a*x
    sage: numerical_integral(f(x,a),0,1, params=[1])
    (0.5, 5.551115123125783e-15)
    sage: numerical_integral(f(x,a),0,1, params=[6])
    (2.9999999999999996, 3.330669073875469e-14)
    
  2. What is not clear is which of the two variables in f is considered by numerical_integral as being the parameter. Is it x or a? Which is the main variable, the first one or the last one, or can it be specified?

  3. A surprise. When you evaluate

    sage: numerical_integral(f(x,a),0,1, params=[a])
    (0.3333333333333333, 3.700743415417188e-15)
    

    it seems that the parameter is transformed into the main variable before integrating: the answer is exactly the integral of $x^2$ (or $a^2$ if the integration is with respect to $a$) from 0 to 1.

    sage: numerical_integral(x^2,0,1,)
    (0.3333333333333333, 3.700743415417188e-15)
    

    Not sure if this behaviour is wanted or a bug.

  4. Weirder (if you thought a was the parameter and x the main variable):

    sage: f(x,a)=a*x^2
    sage: numerical_integral(f(x,a),0,1, params=[1])
    (0.5, 5.551115123125783e-15)
    sage: numerical_integral(f(x,a),0,1, params=[a])
    (0.25, 2.7755575615628914e-15)
    

    Now the integral with parameter set to 1 is wrong, unless integration is with respect to $a$ rather than $x$. With params=[a] it now looks like x was set to a before integrating with respect to a.

  5. Since numerical_integral(f(x,a),0,1, params=[a]) has a fixed value, when you define

    sage: g(x,a) = numerical_integral(f(x,a),0,1, params=[a])
    

    this does not depend on x or a, so g(anything,anything) will always return the same value.

  6. For some reason, the "same" g defined using def doesn't behave the same:

    sage: def g(x,a):
    ....:     return numerical_integral(f(x,a),0,1, params=[a])
    ....: 
    sage: g(x,6)
    (2.9999999999999996, 3.330669073875469e-14)
    

    Here, g(x,6) returns numerical_integral(f(x,6),0,1, params=[6]), in which the function to integrate only has one variable, x, and the optional parameter params=[6] is just ignored.

    You could simply write:

    sage: def g(a):
    ....:     return numerical_integral(f(x,a),0,1)
    ....: 
    sage: g(6)
    (2.9999999999999996, 3.330669073875469e-14)
    
  7. So which one is the main variable?

    In numerical_integral(f(x,a),0,1,params=[value]), the function of several variables is treated as a function of one variable with parameters, but the main variable is a, not x.

    You might think the main variable is the last of the arguments of f(x,a) but it seems that it's really the first variable in the lexicographic order.

    Indeed, compare

    sage: f(x,y)=y*x^2
    sage: numerical_integral(f(x,y),0,1, params=[1])
    (0.3333333333333333, 3.700743415417188e-15)
    

    and

    sage: f(x,a)=a*x^2
    sage: numerical_integral(f(x,a),0,1, params=[1])
    (0.5, 5.551115123125783e-15)
    
  8. Can you change which variable is the main variable and which are parameters?

    I haven't figured that out yet.