Ask Your Question

mforets's profile - activity

2021-01-27 19:55:33 +0100 received badge  Popular Question (source)
2021-01-27 18:33:09 +0100 marked best answer Factoring expression involving exponentials

I've got a complicated expression, which looks like: e(4ik+3iω+ip)−e(4ik+2iω+2ip)−e(4ik+2iω)+e(4ik+iω+ip)+2e(2ik+4iω+ip)−e(2ik+3iω+2ip)−e(2ik+3iω) + ...

How would you do to factor the exponentials with the same power of k, i.e. e(4ik)(...) + e(2ik)(...) and so on?

2021-01-27 18:33:09 +0100 received badge  Self-Learner (source)
2021-01-02 19:27:58 +0100 received badge  Famous Question (source)
2020-12-16 21:49:22 +0100 received badge  Notable Question (source)
2020-12-04 18:04:46 +0100 received badge  Famous Question (source)
2020-09-16 13:23:55 +0100 received badge  Popular Question (source)
2020-08-21 16:28:57 +0100 received badge  Good Answer (source)
2020-06-30 16:42:20 +0100 received badge  Nice Answer (source)
2019-09-21 14:36:31 +0100 received badge  Popular Question (source)
2019-09-21 14:36:31 +0100 received badge  Notable Question (source)
2019-07-23 07:54:11 +0100 received badge  Notable Question (source)
2019-07-09 13:36:17 +0100 received badge  Popular Question (source)
2019-03-14 18:15:03 +0100 received badge  Good Answer (source)
2018-12-03 17:49:11 +0100 received badge  Great Answer (source)
2018-12-03 17:49:11 +0100 received badge  Guru (source)
2018-12-03 17:46:50 +0100 received badge  Nice Question (source)
2018-12-03 17:46:47 +0100 marked best answer Substitution of several variables

Let $f=f(x_1(t),x_2(t))$ be defined as follows:

sage: var('t mu')
sage: x=list()   
sage: x.append( function('x1')(t) )
sage: x.append( function('x2')(t) )
sage: f = x[0] - mu*x[0]^2*x[1]
sage: f

$f = x_1(t) - \mu x_1(t)^2 x_2(t)$.

Now, I need to to substitute the term containing $x_1^2(t) x_2(t)$ in $f$ by some new auxiliary variable $x_3(t)$, that is, to obtain $f = x_1(t) - \mu x_3(t) $. However, this code doesn't work:

sage: x.append( function('x3')(t) )
sage: f.subs({x[0]^2*x[1] : x[2]})

Some ideas? Thanks.

This code is motivated by symbolic manipulation with ODEs (think of $f$ as being the right-hand side term in the autonomous ODE system $\dot{x}(t) = f(x(t))$ ).

2018-11-07 19:40:18 +0100 commented answer finding general term of a sequence

For the OP: Sage ships with the command oeis to play with directly in the REPL, eg.

sage: oeis([1, 4, 36, 288, 7200], max_results=2)
0: A277174: a(n) = Product_{i=1..n} i*rad(i) where rad(n) = A007947(n).
1: A316297: a(n) = n! times the denominator of the n-th harmonic number H(n).
2018-09-30 07:10:20 +0100 received badge  Popular Question (source)
2018-09-30 00:49:46 +0100 commented question solving an iterated optimization problem

minimize_constrained expects [x,0.5,0] to be the initial point for finding the minimum, but the 1st entry, x, is a symbolic expression. Perhaps you can let min_fx = lambda x: minimize_constrained(f, [c_1,c_2,c_3,c_4], [x, 0.5, 0], algorithm='l-bfgs-b') and then operate on min_fx?

2018-09-20 13:17:38 +0100 commented answer div, grad and curl once again

I couldn't resist to comment that the vector calculus example notebooks are delightful!

2018-09-19 14:05:45 +0100 received badge  Nice Question (source)
2018-09-17 21:23:22 +0100 marked best answer Computing polyhedron from MIP

I want to run the following code (I'm using SAGE v6.10 release version 2015-12-18):

P = MixedIntegerLinearProgram()
x = P.new_variable()
A = random_matrix(RR, 3, 2); 
P.add_constraint(A*x <= [2.1,1.5,0.4]) 
P.polyhedron()

However, it outputs the error message:

AttributeError: type object 'float' has no attribute 'fraction_field'

I noticed that if instead of using real variables I use only integer variables, then it works fine. For instance:

P = MixedIntegerLinearProgram()
x = P.new_variable()
A = random_matrix(ZZ, 3, 2); 
P.add_constraint(A*x <= [2,1,0]) 
P.polyhedron()

outputs:

A 2-dimensional polyhedron in QQ^2 defined as the convex hull of 1 vertex and 2 rays

What do you suggest to overcome this problem?

Thanks

2018-06-03 13:38:45 +0100 received badge  Good Answer (source)
2018-03-27 20:48:55 +0100 commented answer Bug in general power of a matrix

Thanks for finding and fixing the bug. Feel free to CC me when you create the ticket.

2018-02-13 21:01:12 +0100 received badge  Nice Answer (source)
2017-11-23 16:28:19 +0100 answered a question Numerical approximation of multiple integral

For numerical integrals in Sage, use numerical_integral. The function integrate is more appropriate for symbolic integration.

With your example:

sage: import scipy.stats as st
sage: numerical_integral(lambda x : st.norm.pdf(x), (-oo, 0.5))
(0.6914624612740132, 1.580169726891987e-09)

The first component is the value of the integral, and the second component is an error estimate.

As far as i'm aware, Sage does not expose a multi-dimensional numerical integration function. This can be worked around by a nested call to numerical_integral, but it will not be as efficient as it could be for some applications. There are more examples in Mathematical Computation with SageMath, Ch. 14, page 315.

2017-11-20 00:44:52 +0100 answered a question Scatter search optimisation

what optimisation algorithms are available.

Check those provided by SciPy's optimization and root finding library. All of them are available through the Sage interpreter by calling the appropriate scipy.optimize function. For example, the basin-hopping algorithm finds the global minimum of a scalar function:

sage: import numpy as np
sage: from scipy.optimize import basinhopping
sage: func = lambda x: np.cos(14.5 * x - 0.3) + (x + 0.2) * x
sage: x0=[1.]
sage: minimizer_kwargs = {"method": "BFGS"}
sage: ret = basinhopping(func, x0, minimizer_kwargs=minimizer_kwargs, niter=200)

                                              fun: -1.0008761844426548
          lowest_optimization_result:       fun: -1.0008761844426548
...

In some (but not all) cases, Sage provides an interface to these algorithms, so that you can directly use Sage symbolic functions as arguments.

It is worth mentioning that "out there" you'll find dozens of Python libraries for mathematical optimization (*). Notice that standard Python packages can be installed with the terminal command

$ sage --pip install --user package_name

and if the installation goes well, it is available the next time you start Sage via import package_name.


(*) See eg.:

2017-11-18 11:06:19 +0100 commented question command to get eigenvalues

this is of course not great -- to post the same question in several places at the very same time! --, but i grant the OP that there is no way to know in advance that roughly the same people are reading both lists: just checked the FAQ and the HELP of this site, and i see no references to the googlegroups lists.

2017-11-16 22:31:40 +0100 commented question how to create a matrix valued function?

actually i also expect that evaluating the symbolic matrix should be available (which currently isn't, as you've shown), in the same way that it works for scalar functions,

sage: f(x, y, z) = x+y+z
sage: f(1,2,3)
6
2017-11-15 11:26:15 +0100 commented answer Problem evaluating a very tiny integral

ok, i see. since Arb is already in C there could be some Cython wrapper for this library that can be used from Sage.