Parameters as array in Yaml in Symfony2 - symfony

I'm doing this which works fine:
parameters:
array_name1: [a, b, c, d]
array_name2: [x, y, a, b]
Now I need to add what in PHP would be $array_name3[1] = array("a", "b", "c") etc., so something like this:
parameters:
array_name3[1]: [1, 2, 3]
array_name3[2]: [a, b, c]
array_name3[3]: [x, y, z]
...which of course doesn't work. Nothing I try seems to be accepted.
How do I define multi-dimensional arrays in Yaml (Symfony2)?

Try this
parameters:
array_name3:
- [1, 2, 3]
- [a, b, c]
- [x, y, z]
Or if you want to have it associative:
parameters:
array_name3:
1: [1, 2, 3]
2: [a, b, c]
bla: [x, y, z]
Or if you want more - read documentation

Related

Symply.py for getting coefficients for ALL combination of the variables of a multivariable polynomial

How to get coefficients for ALL combinations of the variables of a multivariable polynomial using sympy.jl or another Julia package for symbolic computation?
Here is an example from MATLAB,
syms a b y
[cxy, txy] = coeffs(ax^2 + by, [y x], ‘All’)
cxy =
[ 0, 0, b]
[ a, 0, 0]
txy =
[ x^2y, xy, y]
[ x^2, x, 1]
My goal is to get
[ x^2y, xy, y]
[ x^2, x, 1]
instead of [x^2, y]
I asked the same question at
https://github.com/JuliaPy/SymPy.jl/issues/482
and
https://discourse.julialang.org/t/symply-jl-for-getting-coefficients-for-all-combination-of-the-variables-of-a-multivariable-polynomial/89091
but I think I should ask if this can be done using Sympy.py.
Using Julia, I tried the following,
julia> #syms x, y, a, b
julia> ff = sympy.Poly(ax^2 + by, (x,y))
Poly(ax**2 + by, x, y, domain='ZZ[a,b]')
julia> [prod(ff.gens.^i) for i in ff.monoms()]
2-element Vector{Sym}:
x^2
y
This is a longer form rewrite of the one-liner in the comment.
It uses Pipe.jl to write expressions 'functionally', so familiarity with pipe operator (|>) and Pipe.jl will help.
using SymPy
using Pipe
#syms x, y, a, b
ff = sympy.Poly(a*x^2 + b*y, (x,y))
max_degrees =
#pipe ff.monoms() .|> collect |> hcat(_...) |>
reduce(max, _, dims=2) |> vec
degree_iter =
#pipe max_degrees .|> UnitRange(0, _) |>
tuple(_...) |> CartesianIndices
result = [prod(ff.gens.^Tuple(I)) for I in degree_iter] |>
reverse |> eachcol |> collect
or using more of the python methods:
[prod(ff.gens.^I) for
I in Iterators.product((0:d for d in ff.degree.(ff.gens))...)] |>
reverse |> eachcol |> collect
Both give the desired result:
2-element Vector{...}:
[x^2*y, x*y, y]
[x^2, x, 1]
UPDATE:
In case there are more than 2 generators, the result needs to be a Array with higher dimension. The last bits of matrix transposes is immaterial and the expressions become:
Method 1:
max_degrees =
#pipe ff.monoms() .|> collect |> hcat(_...) |>
reduce(max, _, dims=2) |> vec
degree_iter =
#pipe max_degrees .|> UnitRange(0, _) |>
tuple(_...) |> CartesianIndices
result = [prod(ff.gens.^Tuple(I)) for I in degree_iter]
Method 2:
result = [prod(ff.gens.^Tuple(I)) for I in degree_iter]
Thanks a lot #Dan Getz. Your solution works for the TOY example from MATLAB. My real case is more complicated, which has more variables and polynominals. I tried your method for 3 variables,
using SymPy
#syms x, y, z, a, b
ff = sympy.Poly(a*x^2 + b*y + z^2 + x*y + y*z, (x, y, z))
[prod(ff.gens.^Tuple(I)) for I in CartesianIndices(tuple(UnitRange.(0,vec(reduce(max, hcat(collect.(ff.monoms())...), dims=1)))...))]
I got the following error,
ERROR: LoadError: DimensionMismatch: arrays could not be broadcast to a common size; got a dimension with lengths 3 and 5
Stacktrace:
How to generate your method to any number of variables with different degrees, e.g., x^3 + y^3 + z^3 + xyz + xy^2z?
You can find the degree of each of the two variables of interest and then use them to create the matrix of generators; you can use them to get the coefficients of interest. I am not sure what you expect if the equation were like a*x**2 + b*y + c...
>>> from sympy import *
>>> from sympy.abc import a, b, x, y
>>> eq = a*x**2 + b*y
>>> deg = lambda x: Poly(eq, x).degree() # helper to give degree in "x"
>>> v = (Matrix([x**i for i in range(deg(x),-1,-1)]
... )*Matrix([y**i for i in range(deg(y),-1,-1)]).T).T; v
Matrix([[x**2*y, x*y, y], [x**2, x, 1]])
>>> Matrix(*v.shape, [eq.coeff(i) if i.free_symbols else eq.as_coeff_Add()[0]
... for i in v])
Matrix([[0, 0, b], [a, 0, 0]])
From #jverzani (thanks)
using SymPy;
#syms a b x y;
eq = a*x^2 + b*y;
deg = x -> sympy.Poly(eq, x).degree();
xs, ys = [x^i for i ∈ range(deg(x):-1:0], [y^i for i ∈ deg(y):-1:0];
v = permutedims(xs .* permutedims(ys));
M = [x^2*y x*y y; x^2 x 1];
[length(free_symbols(i)) > 0 ? eq.coeff(i) : eq.as_coeff_add()[1] for i ∈ v];
[0 0 b; a 0 0]

Sum all elements in a list without repeating already computed elements

I'm trying to get to the result on what would be a double for-loop in another language (Java or JavaScript, for instance).
So the closest I can come up with is something like this:
1> L = [1,2,3].
[1,2,3]
2> R = [X + Y || X <- L, Y <- L].
[2,3,4,3,4,5,4,5,6]
3>
...but what I do really want is: [3,4,5]. I don't want to sum the elements that were already added:
A1 + A2
A2 + A3
A2 + A1 [already computed, position switched]
A2 + A3 [already computed, position switched]
A3 + A1
A3 + A2 [already computed, position switched]
Thanks in advance...
TL;DR
[X+Y || X <- L, Y <- L, Y > X].
Other solutions
You essentially want two iterators walking alongside the same data structure and an accumulator to collect sums of distinctive elements. There is no reason why you wouldn't be able to mimic such iterators in Erlang:
-module(sum2).
-export([start/1]).
start(Max) ->
L = lists:seq(1, Max),
T = list_to_tuple(L),
do_sum(T, 1, 2, size(T), []).
do_sum(T, X, S, S, A) when X + 1 =:= S ->
lists:reverse([mk_sum(X, S, T) | A]);
do_sum(T, X, S, S, A) ->
do_sum(T, X + 1, X + 2, S, [mk_sum(X, S, T) | A]);
do_sum(T, X, Y, S, A) ->
do_sum(T, X, Y + 1, S, [mk_sum(X, Y, T) | A]).
mk_sum(X, Y, T) -> element(X, T) + element(Y, T).
The result:
7> c(sum2).
{ok,sum2}
8> sum2:start(3).
[3,4,5]
9> sum2:start(5).
[3,4,5,6,5,6,7,7,8,9]
There is actually a simpler solution if you don't have a list of elements that you want to sum but just integers:
-module(sum3).
-export([start/1]).
start(Max) -> do_sum(1, 2, Max, []).
do_sum(X, S, S, A) when X + 1 =:= S -> lists:reverse([X + S | A]);
do_sum(X, S, S, A) -> do_sum(X + 1, X + 2, S, [X + S | A]);
do_sum(X, Y, S, A) -> do_sum(X, Y + 1, S, [X + Y | A]).
Or even a simpler solution with just list comprehension:
4> L = [1, 2, 3].
[1,2,3]
5> [X+Y || X <- L, Y <- L, Y > X].
[3,4,5]
6> f().
ok
7> L = [1,2,3,4,5].
[1,2,3,4,5]
8> [X+Y || X <- L, Y <- L, Y > X].
[3,4,5,6,5,6,7,7,8,9]
Also check this question, Erlang; list comprehension without duplicates, which tackles a similar problem and has more ideas for possible solutions.

Substitution rule on maple

I am slightly new to maple and I am interested in the following question.
Let's assume I have a map f that sends a to abb and b to a. My first question is how do I define this map on maple? I am also interested in when the map is applied repeated i.e. the iterates of f. Say if I have a seed ab, is there something I can put on maple such that when I apply f, it becomes abba and so on?
Thank you
Hope this helps:
> f(a) := a,b,b;
a, b, b
> f(b) := a;
a
> map(f, [a, b]);
[a, b, b, a]
> map(f, %);
[a, b, b, a, a, a, b, b]
You could also do this using strings.
restart:
g := e->StringTools:-SubstituteAll(StringTools:-CharacterMap("ab","ta",e),"t","abb"):
g("a");
"abb"
g("b");
"a"
g("ab");
"abba"
(g##2)("ab");
"abbaaabb"
subs(["a"=a,"b"=b],StringTools:-Explode(g("ab")));
[a, b, b, a]
f(a) := a,b,b;
f(a) := a, b, b
f(b) := a;
f(b) := a
map(f, [a, b]);
[a, b, b, a]
map(f, %);
[a, b, b, a, a, a, b, b]
F := e->map(f,e):
F([a, b]);
[a, b, b, a]
(F##2)([a, b]);
[a, b, b, a, a, a, b, b]
evalb( subs(["a"=a,"b"=b],StringTools:-Explode(g("ab"))) = F([a, b]) );
true
ans1 := CodeTools:-Usage( (g##20)("ab") ):
memory used=6.01MiB, alloc change=4.01MiB, cpu time=100.00ms, real time=102.00ms, gc time=0ns
ans1list := CodeTools:-Usage( subs(["a"=a,"b"=b],StringTools:-Explode(ans1)) ):
memory used=32.00MiB, alloc change=32.01MiB, cpu time=170.00ms, real time=175.00ms, gc time=0ns
ans2 := CodeTools:-Usage( (F##20)([a, b]) ):
memory used=128.11MiB, alloc change=21.02MiB, cpu time=3.81s, real time=3.82s, gc time=3.46s
evalb( ans1list = ans2 );
true
You could also write either g or F to chew on larger chunks (of length 2, or 4, etc), and to recursively split the argument, etc, and even to learn such patterns.

Move every second element to the back of a list, recursively

I'm looking for a way to shuffle a list of numbers in a specific way.
shuffle([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) should return [1, 3, 5, 7, 9, 11, 2, 6, 10, 4, 12, 8]
The recursion would be something like this:
[1,3,5,7,9,11] with remainder [2,4,6,8,10,12]
[2,6,10] with remainder [4,8,12]
[4,12] with remainder [8]
and then you append the result lists and return the wanted answer.
My current code looks like this. How can I adapt it so that it produces the type of recursion I explained above? the mode is shuffle(+,?).
shuffle([], _).
shuffle(List, Shuffled) :- r(List, Shuffled).
r([], []).
r([X], [X]):- !.
r([X,A|Xs], [X|Ys]) :- r(Xs, Ys).
First, a predicate that gets half the work done: reorders the list so that every second element is picked out and appended to the back, keeping the order:
untangle([], []).
untangle([X|Xs], [X|Ys]) :-
untangle_1([X|Xs], [X|Ys], Bs, Bs).
% The rest of the Untangled is the list at the back;
% the list at the back is now empty
untangle_1([], Back, Back, []).
% Keep elements in odd positions at the front
untangle_1([X|Xs], [X|Untangled], Back, Bs) :-
untangle_2(Xs, Untangled, Back, Bs).
% Same as above
untangle_2([], Back, Back, []).
% Move elements in even positions to the back
untangle_2([X|Xs], Untangled, Back, [X|Bs]) :-
untangle_1(Xs, Untangled, Back, Bs).
This is very similar to the interwine/3 defined in this answer. Instead of using two lists for the "unzipped" elements, it puts them at the front and back of the same list.
Now what you need is shuffle the elements that would otherwise be appended to the back:
shuffle([], []).
shuffle([X|Xs], Shuffled) :-
untangle_1([X|Xs], Shuffled, Back, Bs),
shuffle(Bs, Back).
Did I understand that correctly?
?- shuffle([a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z], S), write(S).
[a,c,e,g,i,k,m,o,q,s,u,w,y,b,f,j,n,r,v,z,d,l,t,h,x,p]
S = [a, c, e, g, i, k, m, o, q|...].
You will also notice that this shuffle/2 works in modes shuffle(+List, -Shuffled), shuffle(-List, +Shuffled), and shuffle(?List, ?Shuffled). To what I can see, it is identical in semantics (and almost identical in implementation) to the solution of false.
Here is a version using DCGs:
eo([], Ys,Ys) -->
[].
eo([X|Xs], [X|Ys0],Ys) -->
eo2(Xs, Ys0,Ys).
eo2([], Ys,Ys) -->
[].
eo2([X|Xs], Ys0,Ys) -->
[X],
eo(Xs, Ys0,Ys).
list_shuffled(Xs, Ys0) :-
phrase(eo(Xs, Ys0,Ys),Ys).
And here is the most general query showing all possible uses:
?- list_shuffled(Xs,Ys), numbervars(Xs+Ys,0,_).
Xs = Ys, Ys = []
; Xs = Ys, Ys = [A]
; Xs = Ys, Ys = [A, B]
; Xs = [A, B, C], Ys = [A, C, B]
; Xs = [A, B, C, D], Ys = [A, C, B, D]
; Xs = [A, B, C, D, E], Ys = [A, C, E, B, D]
; Xs = [A, B, C, D, E, F], Ys = [A, C, E, B, D, F]
; Xs = [A, B, C, D, E, F, G], Ys = [A, C, E, G, B, D, F]
; ... .
Here's another, somewhat transparent solution using append:
shuffle([], []).
shuffle([X|T], Shuffled) :-
unzip([X|T], Odd, Even),
shuffle(Even, EvenShuffled),
append(Odd, EvenShuffled, Shuffled).
% Split a list into odd and even elements
unzip([], [], []).
unzip([X], [X], []).
unzip([X,Y|T], [X|Tx], [Y|Ty]) :-
unzip(T, Tx, Ty).
For the record, I do prefer Boris' and false's solutions to this one (+1 to both) as both are more efficient. :)

Rectangular Peg Solitaire in Prolog?

possible quick question here since I'm new to Prolog. I'm trying to convert this code for solving a triangular peg solitaire puzzle into solving a rectangular peg solitaire puzzle. The problem I think I'm facing is trying to figure out how to let the program know it completed the puzzle. Here's what I've got currently:
% Legal jumps along a line.
linjmp([x, x, o | T], [o, o, x | T]).
linjmp([o, x, x | T], [x, o, o | T]).
linjmp([H|T1], [H|T2]) :- linjmp(T1,T2).
% Rotate the board
rotate([[A, B, C, D, E, F],
[G, H, I, J, K, L],
[M, N, O, P, Q, R],
[S, T, U, V, W, X]],
[[S, M, G, A],
[T, N, H, B],
[U, O, I, C],
[V, P, J, D],
[W, Q, K, E],
[X, R, L, F]]).
rotateBack([[A, B, C, D],
[E, F, G, H],
[I, J, K, L],
[M, N, O, P],
[Q, R, S, T],
[U, V, W, X]],
[[D, H, L, P, T, X],
[C, G, K, O, S, W],
[B, F, J, N, R, V],
[A, E, I, M, Q, U]]).
% A jump on some line.
horizjmp([A|T],[B|T]) :- linjmp(A,B).
horizjmp([H|T1],[H|T2]) :- horizjmp(T1,T2).
% One legal jump.
jump(B,A) :- horizjmp(B,A).
jump(B,A) :- rotate(B,BR), horizjmp(BR,BRJ), rotateBack(A,BRJ).
%jump(B,A) :- rotate(BR,B), horizjmp(BR,BRJ), rotate(BRJ,A).
% Series of legal boards.
series(From, To, [From, To]) :- jump(From, To).
series(From, To, [From, By | Rest])
:- jump(From, By),
series(By, To, [By | Rest]).
% A solution.
solution(L) :- series([[o, x, x, x, x, x],
[x, x, x, x, x, x],
[x, x, x, x, x, x],
[x, x, x, x, x, x]], L).
The triangular puzzle code required that the user input what the ending table would look like, but I didn't want that. I want this to show any possible solution. The table will always be exactly 6x4. I liked the idea of rotating the grid to continue to simply figure out horizontal jumps, so I changed the rotate function to rotate it's side, and added a RotateBack function to put it back into place. I figured I would have to do this because the grid isn't symmetrical. Since it will always be this size, I figure the simplest way to find the end is to set up a counter that will count how many moves are taken place. Once we hit 22 moves (the max moves possible to clear the whole grid except for 1 peg), then the solution will be a success.
In other words, I think I need to remove this code:
% Series of legal boards.
series(From, To, [From, To]) :- jump(From, To).
series(From, To, [From, By | Rest])
:- jump(From, By),
series(By, To, [By | Rest]).
And change it so that it sets up a counter that stops at 22. Any suggestions?
I think you could count the pegs, or better, fail when there are at least 2.
To do it efficiently, should be (untested code)
finished(L) :-
\+ call_nth(find_peg(L), 2).
find_peg(L) :-
member(R, L),
memberchk(R, x).
call_nth/2, as defined in this answer, requires the builtin nb_setval. This is available in SWI-Prolog or Yap.

Resources