Boolean Logic - anything complemented twice is equal to original for multiple variables - boolean

I know that A + A'B == A + B but does it work for B' + BC'D = B' + C'D ?
Also, am I able to do A' + AB = A' + B?
Where A' is the negation of A.

I know that A + A'B == A + B but does it work for B' + BC'D = B' + C'D ?
Yes. The two equations are effectively the same, just with different names for variables.
A in first equation = B' in second equation.
B in first equation = C'D in second equation.
Also, am I able to do A' + AB = A' + B?
Yes. Again, the two equations are effectively the same, just with different variable names.
A in first equation = A' in third (most recent) equation.
For future reference... when in doubt, just make a truth table (or k-map, if necessary) to figure out the simplest equivalent solution.

Related

how to pass parameters by reference in matlab function

i'm a beginner in matlab and i want to calculate the sum of product of every possible combination of elements of a (3*n) matrix that are picked from different row.
for example if the matrix is x = [1 2 3 , 4 5 6] i want the result of
D = 1*4 + 1*5 + 1*6 + 2*4 + 2*5 + 2*6 + 3*4 + 3*5 + 3*6.
I wrote the following recursive code but i'm having problem with passing a variable by reference.
function combination(n,A,x) % n= number of rows ,A= empty array, x = the matrix
if n == 0
D = D + prod(A);
else
for i = 1:1:3
A = [A x(n,i)];
combination(n-1,A,x);
if length(A)>=1
A = A(1:length(A)-1);
end
end
end
end
i need the D parameter but when i declare D as global it doesn't help.
is there anyway in matlab that i could pass D by reference in function and get the result at the end?
thanks in advance.
sorry for my english.
Can you just use prod(sum(x,2))? I think if you rearrange the terms in your sum, you'll see that you can just multiply the sums of your rows together and you'll get the same thing. (But perhaps I've misunderstood exactly what you're looking for).
For example:
>> x=[1 2 3 ; 4 5 6; 7,8,9]
x =
1 2 3
4 5 6
7 8 9
>> prod(sum(x,2))
ans =
2160
>> 1*4*7 + 1*4*8 + 1*4*9 + 1*5*7 + 1*5*8 + 1*5*9 + 1*6*7 + 1*6*8 + 1*6*9 + 2*4*7 + 2*4*8 + 2*4*9 + 2*5*7 + 2*5*8 + 2*5*9 + 2*6*7 + 2*6*8 + 2*6*9 + 3*4*7 + 3*4*8 + 3*4*9 + 3*5*7 + 3*5*8 + 3*5*9 + 3*6*7 + 3*6*8 + 3*6*9
ans =
2160
If you really need to do this recursively, using a combinatory approach, you should be able to just pass in D as an input and also return it as an output of your function, something like this:
function D = combination(n,A,x, D) % n= number of rows ,A= empty array, x = the matrix
if n == 0
D = D + prod(A);
else
for i = 1:1:3
A = [A x(n,i)];
D = combination(n-1,A,x, D);
if length(A)>=1
A = A(1:length(A)-1);
end
end
end
end
and then call it initially with D equal to zero.
Don't worry about passing by reference here. MATLAB doesn't have pass by reference (it has variables that have reference semantics, but that's a different thing), but it uses copy-on-write, and has special optimizations for calculations that can be done in-place, as your calculation with D can here.
Declaring it as global does the job, however, you should pass it as input, and return it as output.
MATLAB will take care of not making a copy if your code does not need a copy of it.
If you write in the function definition:
function D = combination(n,A,x,D)
D = D + something
end
your function can be called by:
output = combination(n_in,A_in,x_in
the recursion follows the same so instead of calling
combination(n-1, A,x)
you write
D = combination(n-1,A,x,D)
and go on in your code.
I hope this helps you, and maybe there is a Matlab own function that can handle this task already but I am not sure.

MATLAB Simple Calculation

I am working on MATLAB on my own, and was doing problem 9 on Project Euler
It states
" A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc."
Below is the code I wrote; however, it compiles, but does not produce and output. I was hoping to get some feedback on what's wrong, so I can fix it.
Thanks,
syms a;
syms b;
syms c;
d= 1000;
d= a + b + c ;
ab= a.^2 + b.^2;
ab= c.^2;
c
I propose a vectorized way (that is, without using loops) to solve the problem. It may seem relatively complicated, especially if you come from other programming languages; but for Matlab you should get used to this way of approaching problems.
Ingredients:
Vectorization;
Indexing;
Transpose;
Implicit singleton expansion;
hypot;
find.
Read up on these concepts if you are not familiar with them, and then try to solve the problem yourself (which of course is the whole point of Project Euler). As a hint, the code below proceeds along these lines:
Generate a 1×1000 vector containing all possible values for a and b.
Compute a 1000×1000 matrix with the values of c corresponding to each pair a, b
From that compute a new matrix such that each entry contains a+b+c
Find the row and column indices where that matrix equals 1000. Those indices are the desired a and b (why?).
You'll get more than one solution (why?). Pick one.
Compute the product of the obtained a and b and the corresponding c.
Once you have tried yourself, you may want to check the code (move the mouse over it):
ab = 1:1000; % step 1
cc = hypot(ab,ab.'); % step 2
sum_abc = ab+ab.'+cc; % step 3
[a, b] = find(sum_abc==1000); % step 4
a = a(1); b = b(1); % step 5
prod_abc = a*b*cc(a,b); % step 6

Polynomial expansion: Separating polynomial coefficients and x's

I want to automatically calculate expansions of polynomials where there are variables (x1,x2,...) as well as coefficients (c1,c2, ...). My goal is to calculate p(1)*(c1*x1+c2*x2+...)^n+ ... + p(n)*(c1*x1+c2*x2+...)^n .
As you can notice the resulting expression can be written as F(x1,x2...)*g(c1,c2,...) [where F is a row matrix and g is column matrix], i.e. there is some multiplicative decoupling between the coefficients and the variables.
Right now I use the MATLAB symbolic toolbox and construct F and g by manually examining the resulting symbolic expansions. This is not very feasible as if n is big and c=(c1,c2,...) is too big there are too many terms and it is no longer possible manually. For instance for (c1*x1+c2*x2+c3) and n=2, what I want is following.
>> p=[2 5]
p =
2 5
>> syms c1 c2 c3
>> syms x1 x2
>> expression= p(1)*(c1*x1+c2*x2+c3)^2 + p(2)*(c1*x1+c2*x2+c3);
>> expand(expression)
ans =
2*c1^2*x1^2 + 4*c1*c2*x1*x2 + 4*c1*c3*x1 + 5*c1*x1 + 2*c2^2*x2^2 + 4*c2*c3*x2 + 5*c2*x2 + 2*c3^2 + 5*c3
>> F=[5*x1 5*x2 5 4*x1*x2 4*x1 4*x2 2*x1^2 2*x2^2 2]
F =
[ 5*x1, 5*x2, 5, 4*x1*x2, 4*x1, 4*x2, 2*x1^2, 2*x2^2, 2]
>> g=[c1 c2 c3 c1*c2 c1*c3 c2*c3 c1^2 c2^2 c3^2].'
g =
c1
c2
c3
c1*c2
c1*c3
c2*c3
c1^2
c2^2
c3^2
>> expand(F*g)
ans =
2*c1^2*x1^2 + 4*c1*c2*x1*x2 + 4*c1*c3*x1 + 5*c1*x1 + 2*c2^2*x2^2 + 4*c2*c3*x2 + 5*c2*x2 + 2*c3^2 + 5*c3
I have found the following question and it looks like there may be a way to do it automatically using conv etc. If one can come up with an automated solution (or at least some idea towards such automation) for the case where x=(x1,x2) and c=(c1,c2,c3) and n=2, the case depicted above; I guess I may be able to generalize it to higher dimensional cases.
Note: the ordering of terms in F or g does not matter, given that they are ordered in some structured way.
The coefficients from different terms don't overlap. The first term, p(1)*(c'*x)^1, has only terms of degree 1 in xi and ci, and so on. So it becomes a matter of computing the coefficients of one term at a time.
That, too, has a "simple" expression:
p(k)*(c'*x)^k = sum(i1,..,im>=0 with sum(i_)=k)
M(k;i1,..,im)*x1^i1*...*xm^im * c1^i1*...*cm^im
where the summation is such that the sum of all i equals k, and M is the multinomial coefficient.
For m=3, n=2, the i's would be in the order of your example: 110,101,011,200,020,002. M(2;110)=2 so the first term is `p(2)*M(2;110)*x1*x2 = 4*x1*x2'.
Your F and g terms are:
F(...) = p(k)*M(k;i1,..,im)*x1^i1*...*xm^im
g(...) = c1^i1*...*cm^im

Get values for 2 arguments from 1 equation

I have this equation: p = (1 + (q-1)*B*T) ^ (-1/q-1)
The values p, T are known and the diagram p - T makes curbe. I want to calculate q and B so that curbe be as close to straight line is posible.
Some values are:
T p
1 0,999147061
2 0,997121331
3 0,994562513
Is there any way to make matlab (or sth else) to give me the values of B and q ?

What does this prime symbol do - MATLAB?

I am working with some matlab code I inhereted from another person, I dont understand the meaning of the line q =[q; qi']. I feel like i should be able to just remove it, so that q = distribuc...
function [ q ] = ObtainHistogramForEachTarget( state, numberOfTargets, image, q )
for i=1 : numberOfTargets
qi = distribucion_color_bin_RGB2(state(i).xPosition,state(i).yPosition,state(i).size,image,2);
q = [q; qi'];
end
end
Can anyone explain this to me?
MATLAB has several built-in functions to manipulate matrices. The special character, ', for prime denotes the transpose of a matrix.
The statement A = [ 1 2 3;4 5 6;7 8 9]' produces the matrix
A =
1 4 7
2 5 8
3 6 9
hope this helps
From Matlab's help
help ctranspose
' Complex conjugate transpose.
X' is the complex conjugate transpose of X.
B = ctranspose(A) is called for the syntax A' (complex conjugate
transpose) when A is an object.
The [X ; Y] syntax concatenates two matrices vertically. So that line is adding the just-computed results to the already computed q. If you simply reassigned q, you would be discarding all the computations the function had already done each time through the loop.
The forward apostrophe ' does a complex conjugate and transposes a matrix. I would guess that distribucion_color_bin_RGB2 probably returns a real-valued column vector, and the author wanted to flip it to horizontal before appending it to the results matrix.
As #ja72 pointed out, it's better style to use .' (just transpose) by default and ' only when you actually mean a complex conjugate, even if you expect your data to be real.
usually A' is the transpose of matrix A, but it is conjugate transpose. it works for real matrix, doesn't work for complex matrix
transpose(A) is the real transpose function, both work for R matrix and C matrix.
I usually use A', it's easy, but I changed my habit until I face bug in FFT transformation
I came across the same problem and tested it using octave(matlab in ubuntu), and found that to a just complex number a, a' means its conjugate.
octave:2> a = 1 + 1j
a = 1 + 1i
octave:3> a'
ans = 1 - 1i
Besides, to a complex matrix A:
octave:6> A = [1 + 2j 1 - 2j ; 2 - 1j 2 + 1j]
A =
1 + 2i 1 - 2i
2 - 1i 2 + 1i
octave:7> A'
ans =
1 - 2i 2 + 1i
1 + 2i 2 - 1i