Sympy : order of solutions from solve - sympy

Are solutions from sympy solve() being ordered in some way? Is it from minimal to maximal solutions?
How can I enforce non-negativity of the solution?
In my problem I need unique minimal positive solution. I appreciate all the help

They aren't ordered, although if they are numeric you can use sorted on them (this will fail if they are symbolic or nonreal). You can force positive only solutions by setting the symbol you solve for as positive, like
x = symbols('x', positive=True)
Also note the caveat with sympy.solve that it is no way guarantees that it has given you all the solutions to an equation---only those which it was able to find with the algorithms that are implemented.

Related

Sympy Solver returing and expression

So I'm taking the determinant of a matrix, then trying to use the Solver in Sympy to solve the expression for a particular variable.
The determinant I am trying to solve while being pretty complicated only has this one variable in it. Being a long expression I don't want to paste it all in but I've shortened it to a snippit that gives the same result;
Determinant = -0.0134365566406344*Nperp**7*sqrt(Nperp**2 + 0.3249)/(3.07787011388119*Nperp**2*sqrt(3.07787011388119*Nperp**2 + 1) + sqrt(3.07787011388119*Nperp**2 + 1)) - 4.2064522609332*Nperp**6/(3.07787011388119*Nperp**2 + 1)
Solutions = solve(Determinant, Nperp**2)
The problem is that when I print Solutions, I get an expression back in terms on Nperp instead of a numerical solution which is what I want.
I'm not sure whether the problem is that Sympy cannot handle the high powers in the polynomial, or if maybe there is no numerical solution possible but I would appreciate some thoughts of people more knowledgeable than I.
Thanks!
Edit: Code not indented
You are solving for Nperp**2. The answers it gives you are correct: they do equal Nperp**2 according to your Determinant equation, but it's probably not what you want. If you give solve an expression, rather than a single symbol, it will just isolate that expression.
You are probably looking for
Solutions = solve(Determinant, Nperp)
For me, this gives two solutions, [-549.228571428573, 0.0].

Using Brent algorithm to find the root of a function f with an initial guess, but without intervals [a,b] s.t. f(a)f(b)<0

I would like to know how to use Brent algorithm if no opposite signs can be provided.
For example, in the C++ library of Brent algorithm, the root-finding procedure that implements Brent’s method has to be used, following the header file, in the form of
double zero ( double a, double b, double t, func_base& f );
where a, b satisfies the condition of opposite signs: f(a).f(b) < 0
In my problem setting, I need to find the root(s) of a black-box function f. An initial guess is provided but no endpoints a,b, such that f(a) f(b)<0 are provided It seems that in Matlab there is a function fmin that only needs an initial guess. I would like to know how to do this using C++, in particular, using the implementation of Brent such as the one linked above?
Thanks for your ideas.
Without doing exhaustive search (and in the case of real valued function, you cannot, since the value of x is uncountable), there is no way to really guarantee finding the root if such exist.
One heuristic approach to address the problem is using gradient descent, in order to minimze (/maximize) the value of the function, until you find a local minimum (/maximum) or until you find a root.
The problem with this approach is you can get stuck in a local minimum (/maximum) before finding the root, and "think" there is no root, even if one does exist.
Under the assumptions that
f is a black-box, i.e. it can be evaluated but no information on its shape is known whatsoever.
You have to use a method that requires a priori knowledge of an interval [a,b] which brackets a root of f (assuming f is continuous).
I think your only option is to make a preliminary search for two valid points a and b.
This can be done in a number of ways. The most simple-minded could be to run a linear search (with some prescribed step) starting from your initial guess, which can be repeated with a finer step if it turns out unsuccessful. If f is not too "weird" a simple method should do.
Clearly, some basic clue on the properties of f is always necessary, for example that it actually has a root and that it is continuos, differentiable, etc.. All root finding methods (gradient descent, Newton-Raphson, bisection, etc.) assume some basic properties of the function.

Why is SymPy's solver returning only one, trivial solution?

I want to find all the real numbers that satisfy a particular equation. I have no trouble finding these values in Mathematica with
Solve[n*9^5 == 10^n-1, n]
which gives both 0 and 5.51257; but when I use SymPy's (0.7.3; Python 2.7.5) solve
n = sympy.symbols('n')
sympy.solve(n*9**5 - 10**n-1, n)
I seem to only get something that looks like 0, and not the second value, which is what I'm really seeking.
How can I get SymPy to produce the non-trivial solution I'm looking for? Is there a different function or package I should be using instead?
solve only gives symbolic solutions, so if it cannot find a closed form for a solution, it will not return it. If you only care about numeric solutions, you want in SymPy is nsolve, or you can use a more numerical oriented Python library. For example
sympy.nsolve(n*9**5 - 10**n-1, n, 5)
will give you the solution you are looking for.
The problem with using solve is that there are infinitely many solutions, each corresponding to a branch of the LambertW function. See WolframAlpha for the full solution set. Unfortunately, only the principal branch of LambertW is implemented in SymPy.
Until this is fixed, another way to fix the issue would be to manually evaluate the LambertW returned by solve on another branch, using mpmath.lambertw. The easiest way to do this is with lambdify:
s = sympy.solve(n*9**5 - 10**n-1, n)
import sympy.mpmath
# Replace -1 with any integer. -1 gives the other real solution, the one you want
lambdify([], s, [{'LambertW': lambda x: sympy.mpmath.lambertw(x, -1)}, "mpmath"])()
This gives [mpf('5.5125649309411875')].
The dictionary tells lambdify to evaluate the LambertW function using the -1 branch, using mpmath. The "mpmath" tells it to use mpmath for any other functions that may be in the solution.

OCaml - issues connected with using fold__

I have a several questions connected with fold_left/right.
How to accumulate two or more values? If using a tuple is good solution ?
How to abort work of fold ? For example we must find firstly occurrence of any number, and return position. I mean a break (C++).
zurgl's comment is a great answer (maybe move it down to the answer region).
You can use an exception to terminate a fold early. It's good to try to structure your code so you don't have to do this (in my opinion). Code without exceptions is easier to understand, more composable, parallelizable, etc.
we must find firstly occurrence of any number then you must traverse all your list [1;1;1;1;5], no need to abort here. To deal with partial recursion there are other function like dropwhile, takewhile ... Or you can define the one you need. See folding as projection between type, you have a source type and a target type with a seed value (the accumulator), then folding is a procedure which realize this transformation. Yes using tuple is good solution, IMO.

Ocaml: How to get list of every combinations of permutation of two lists?

I have two lists, let's say for example :
let x = [1;2];;
let y = [true;false];;
I want to essentially have a (int*boolean) list list with
[[(1,true);(2;false)];[(1,true);(2,true);];[(1,false);(2,true);];[(1,false);(2,false);]]
Anyone have any idea how to do this?
The question is not specified very well as to what exactly is the operation. What about [(1,true);(1,false)]? It seems that the operation is not symmetric -- the things in x always appear in that order, while the things in y can repeat and appear in any order. Maybe it would help if you break it down into two tasks:
Every permutation with repetition of the second list
Zip every result with the first list
I think nobody is answering because this kinda looks like a homework problem. I would start by considering just the second elements of all the pairs in your desired result. I see a pretty regular structure: TF, TT, FT, FF. It would be a little more regular in the order (say) FF, FT, TF, TT. Anyway once you can make a list of lists that looks like this, it's pretty easy to pair up lists with other lists using functions from the List module.
(You might get more answers if you showed some things you tried and explained why they didn't work.)
Regards,
You can consult List module's document
Otherwise, just type
$ cd `ocamlc -where`
$ less list.mli
You may find functions for what you want to do.