Get a^ib^jc^k from expand (a+b+c)^n - maple

I want to write a function func(n) get a^ib^jc^k from expand (a+b+c)^n.
Example :
When n = 1 then (a + b + c)^1 = a + b + c and func(n) return {a, b, c}.
When n = 2 then (a + b + c)^2 = a^2 + b^2 + c^2 + 2ab + 2bc + 2ca and func(n) return {a^2, b^2, c^2, ab, bc, ca}.
I have solution using three loops but in think it's not nice, someone can help me. Thanks you very much.

In maple to do some sort of programming you can use proc short for procedure.
restart:
ftn:= proc(n)
expand((a+b+c)^n);
{op(%)};
end;
Now let's try to check whether we get what you want.
ftn(1);
{a, b, c}
ftn(2);
{a^2, b^2, c^2, 2*a*b, 2*a*c, 2*b*c}
If you do not want the constant coefficients of ab, ac and bc then you can do this
restart:
ftn:= proc(n)
expand((a+b+c)^n);
convert([coeffs(%,[a,b,c],'powers')],array):
convert({powers},list);
end;
ftn(2);
[a^2, b^2, c^2, ab, ac, b*c]

Related

Recovering the name of a variable in Maple

I am programming a procedure in Maple. This procedure receive a list of vector fields (from the ``DifferentialGeometry'' package). I want that in the output they appear, together with the performed computations, the name of the vector fields introduced by the user.
During the procedure I refer to them by the name I have given to the parameter, but I don't actually know the "names of the variables". Is there a way to recover them? I have being looking for in the Maple documentation and in the DGinfo help, but I didn't get anything.
EXAMPLE ADDED
I have simplified the problem to the following. Consider the code:
with(DifferentialGeometry);
DGsetup([x, u], M);
X := evalDG(D_u*x+2*D_x);
myproc := proc (var)
return evalDG(var+D_u)
end proc;
myproc(X)
The output is
But I want modify the code in such a way that the output were something like
X+D_u is 2 D_x + (1+x) D_u
That is, I want to use the name ("X") of the variable in the output, not only the value (2 D_x + x D_u).
Thank you for your time.
Here is one way of handling your example.
restart;
with(DifferentialGeometry):
DGsetup([x, u], M):
myproc := proc(var::uneval) local evar;
evar := eval(var);
return evalDG(var+D_u) = evalDG(evar+D_u);
end proc:
X := evalDG(D_u*x+2*D_x):
myproc(X);
X + D_u = 2 D_u + (1 + x) D_u
Here is a variant on that idea, with two such parameters on the procedure, but also handling then in a more general manner.
restart;
with(DifferentialGeometry):
DGsetup([x, u], M):
myproc := proc(var1::uneval, var2::uneval)
local evars, res;
evars := [var1=eval(var1), var2=eval(var2)];
res := var1 + var2 + D_u;
return res = evalDG(eval(res, evars));
end proc:
X1 := evalDG(D_u*x+2*D_x):
X2 := evalDG(D_u*x+3*D_x):
myproc(X1, X2);
X1 + X2 + D_u = 5 D_x + (1 + 2 x) D_x

How do I create a simplified logic circuit of this given [(A’B’)’ + (A’+ B’)’]’ ? and What is the simplified Boolean expression?

I have to draw the simplified logic circuit of this given [(A’B’)’ + (A’+ B’)’]’ and also get the simplified boolean expression
From DeMorgan Theory
((A'B')' + (A'+ B')')'=(A+B + AB)'
(A+B + AB)' = (A+B)'. (AB)'
(A+B)'. (AB)' = (A'.B') .(A'+B')
assume
X=A' ,Y=B'
we can conclude that
(XY)(X+Y) is (XY)
as both (XY) and (X+Y) has to be 1 to produce 1 at output as seen from truth table of (XY)(X+Y) is (XY) is identical to XY
so as final result
((A'B')' + (A'+ B')')'=(A+B + AB)'
(A+B + AB)' = (A+B)'. (AB)'
(A+B)'. (AB)' = (A'.B') .(A'+B') = A'B'
before simplifying
after simplifying
comparing outputs to make sure
(A'B')' = A + B
Using DeMorgan's theorem (AB)' = A' + B'
(A'+B')' = AB
Again, using DeMorgan's theorem (A+B)' = A'B'
Therefore, now we have the expression:
(A+B + AB)'
Taking A+B as X and AB as Y
(X+Y)' = X'Y'
= (A+B)'·(AB)'
Now, creating a logic circuit is fairly simple for this expression, inputs A and B are fed to a NOR gate and NAND gate simultaneously whose outputs act as input to an AND gate

q/KDB - nprev function to get all the previous n elements

I am struggling to write a nprev function in KDB; xprev function returns the nth element but I need all the prev n elements relative to the current element.
q)t:([] i:1+til 26; s:.Q.a)
q)update xp:xprev[3;]s,p:prev s from t
Any help is greatly appreciated.
You can achieve the desired result by applying prev repeatedly and flipping the result
q)n:3
q)select flip 1_prev\[n;s] from t
s
-----
" "
"a "
"ba "
"cba"
"dcb"
"edc"
..
If n is much smaller than the rows count, this will be faster than some of the more straightforward solutions.
The xprev function basically looks like this :
xprev1:{y til[count y]-x} //readable xprev
We can tweak it to get all n elements
nprev:{y til[count y]-\:1+til x}
using nprev in the query
q)update np: nprev[3;s] , xp1:xprev1[3;s] , xp: xprev[3;s], p:prev[s] from t
i s np xp1 xp p
-------------------
1 a " "
2 b "a " a
3 c "ba " b
4 d "cba" a a c
5 e "dcb" b b d
6 f "edc" c c e
k equivalent of nprev
k)nprev:{$[0h>#y;'`rank;y(!#y)-\:1+!x]}
and similarly nnext would look like
k)nnext:{$[0h>#y;'`rank;y(!#y)+\:1+!x]}

Simplifying boolean algebra (XOR)

I know how to convert first and second term to the first term of the simplified expression, but I don't know how to convert the rest.
By simplifying, I can get rid of A_Bar in the third term and A in the fifth term and get =B*C_bar
How is it that B*C_bar + the fourth term = becomes XOR(B,C) ?
The two expressions are clearly the same. This can be easily proven by truth tables.
The first one is:
And the second one:
However, this does not fully answer your question.
B*C_bar + the fourth term = becomes XOR(B,C)
This is clearly true if A is true, since per definitionem, B XOR C = B_bar and C OR B and C_bar.
If A is false, these terms are always false and you cannot simplify these two to B XOR C! They are not equal!
Note: Tables generated with http://web.stanford.edu/class/cs103/tools/truth-table-tool/
Note2: ^= OR, ¬ = NOT, ∨ = AND
let play a game.
Let a=not(A), b=not(B) and c=not(C) and *=xor
Y = ab + (B*C)
Y = ab + Bc + bC
Y = ab(1) + Bc(1) + bC(1)
Y = ab(c+C) + Bc(a+A) + bC(a+A)
Y = abc + abC + Bca + BcA + bCa + bCA
Y = abc + abC + aBc + ABc + abC + AbC
Y = abc + abC + aBc + ABc + AbC
That is the first equ.

how to solve this boolean algrbra expression

I would like help simplifying this boolean algebra expression:
B*C + ~A*~B + ~A*~C => A*B*C + ~A
I need to know the steps of how to simplify it to the ABC + ~A
'*' indicates "AND"
'+' indicates "OR"
"~A" indicates "A NOT"
Any help would be appreciated!
Thank you!
For a better view, i'll skip * for conjunction, and use ' for negation.
First you shall expand the 2 term disjunctions: Expand B*C , A'*B' and A'*C'
1) (A + A')BC + A'B'(C + C') + A'(B + B')C'
now distribute the parentheses.
2) ABC + A'BC + A'B'C + A'B'C' + A'BC' + A'B'C'
the fourth term and the last term are the same, A'B'C', so ignore one of them since p + p = p or you can expand the situation for your needs (might be needed for some situations) as in p+p+p+p+....+p = p
3) So now, lets try to search for common terms. See the 2nd term and 5th term, A'BC and A'BC'. Take common parenthesis, A'B(C+C') => A'B.
Do the same for 3rd term and the 4th term, A'B'C and A'B'C'. A'B'(C+C') => A'B' since X+X' = 1.
now we have:
ABC + A'B + A'B'
4) take common parenthesis again, 2nd and 3rd term: A'(B+B')
There you have ABC + A'
BC + A'B' + A'C' => ABC + A'