trouble with python double iteration - python-2.7

Hi and thanks in advance for the help.
I am a beginner in Python and I am having issues with iteration. I understand itertools is probably part of the solution to my problem but I don't seem to formulate it right and I couldn't find a problem similar enough on the forums.
Based on a numpy indices vector V, another vector Y and some function f, I would like to compute something like:
for i in V[:-1]:
for j in V[i+1:]:
f(Y[i], Y[j])
... which as you know doesn't work because you understand python better than I do! It seems never to get to "j" (V not being iterable twice is what I understood so far, although I'm not sure what it means).
What would be a good way to do that? Note that it is quite important not to compute both f(Y[i],Y[j]) and f(Y[j],Y[i]) since it is symmetric and quite long to run.
Thanks!

Related

What is the actual algorithm of this permutations code (in plain English)?

A bit of background first:
I am an amateur programmer, with a background of Mathematica (and more recently Haskell) programming revolving mostly around Codeabbey problems.
Recently I have been trying to implement a permutations generation code with repetition, that is a program which, given an input number, will produce a list of permutations taking into account duplicate numbers. So for example if I input 58883 it will output a list of only 20 numbers (5!/3!), not 120.
It has been brought to my attention that the code in this answer https://stackoverflow.com/a/26716166/2579629 does exactly that, and I can confirm that it does so extremely well resource-wise, because I can get as far as compiling with g++ and running it.
Other than that I am clueless when it comes to C and I can't understand how the code works. I believe that the core of the code is in these few lines
while (*q) {
if (used.find(*q) == used.end()) {
std::swap(*p, *q);
perm(str, index + 1);
std::swap(*p, *q);
used.insert(*q);
}
that recursion is used and that a concept of Sets is used. Other than that I'm lost.
So, can someone explain to me how this algorithm is supposed to work, meaning an explanation of what is going on, in steps that the computer is supposed to follow, that is in plain English, independent of language syntax, and what purpose do Sets serve in this case?
Much obliged! Have a good day!

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].

Simple Code Optimisation

I am a quite new programmer, and I sometimes have really dumb questions,
In a few weeks I am supposed to give back this big semester project and I would have liked a bit of help for my optimization.
Somewhere I needed to get a Quantitiy (class derived from a double) and strip it down to a just a number without integers and print it in a window (I dont have the slightest clue how the latter works, it was given to us by the teacher, but it's not the problem here).
And so I created two variables to do so, which gave me something like this:
int lil_patate=q_nutriments;
string patate(to_string(lil_patate));
And I would have like to set that in a single line, writing that;
string patate(to_string(int lil_patate=q_nutriments));
which of course doesnt work, as I expected, but I would have loved a bit of help to get something working that would be simpler than the first version but doing the same thing,
Thanks for the help and have a nice day :)
Humphrey
If you need to be able to reference lil_patate elsewhere in your code then you can't make this factorisation at all. If you don't need to refer to lil_patate elsewhere then get rid of it and initialise patate directly from q_nutrients:
string patate(to_string(q_nutriments));
However, while this may improve the readability of the code, it doesn't represent an optimisation in any technical sense.

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