How to define the size between multiple symbolic variables and sort these symbolic elements

3 ビュー (過去 30 日間)
jm
jm 2024 年 6 月 18 日
コメント済み: Paul 2024 年 6 月 19 日
for example:
syms c1 c2 c3; abs(c1) >= abs(c2) >= abs(c3)
a = 1/4 - c2/4 - c3/4 - c1/4;
b = c2/4 - c1/4 + c3/4 + 1/4;
c = c1/4 - c2/4 + c3/4 + 1/4;
d = c1/4 + c2/4 - c3/4 + 1/4;
A = [a,b,c,d];
A_asc = sort(A)
Obviously, this is not the answer I wanted. The answer I want is [1/4 - c2/4 - c3/4 - c1/4,c2/4 - c1/4 + c3/4 + 1/4,c1/4 - c2/4 + c3/4 + 1/4,c1/4 + c2/4 - c3/4 + 1/4]
  3 件のコメント
Taylor
Taylor 2024 年 6 月 18 日
Your second assume will need to be assumeAlso because a second call to assume will override the first.
Paul
Paul 2024 年 6 月 19 日
編集済み: Paul 2024 年 6 月 19 日
In addition to the comments/answers about the assumptions, it should be noted that sort does not sort the elements of A in a mathematical order.
Also, it's not clear why the desired answer is just that, at least not based on the information provided.

サインインしてコメントする。

回答 (3 件)

Taylor
Taylor 2024 年 6 月 18 日
Define your constraints using assume and assumeAlso
syms c1 c2 c3;
assume(abs(c1) >= abs(c2));
assumeAlso(abs(c2) >= abs(c3))
a = 1/4 - c2/4 - c3/4 - c1/4;
b = c2/4 - c1/4 + c3/4 + 1/4;
c = c1/4 - c2/4 + c3/4 + 1/4;
d = c1/4 + c2/4 - c3/4 + 1/4;
A = [a,b,c,d];
A_asc = sort(A)

Steven Lord
Steven Lord 2024 年 6 月 18 日
The second part of your first line of code (that I've broken in two) doesn't do what I believe you think it does.
syms c1 c2 c3;
abs(c1) >= abs(c2) >= abs(c3)
ans = 
This does not tell MATLAB to assume that the absolute value of c1 is greater than the absolute value of c2 which is greater than the absolute value of c3. That's what I believe you expected it to do. If you want MATLAB to make assumptions about the values of the variables, you need to call the assume and/or assumeAlso functions.
assume(abs(c1) >= abs(c2))
assumeAlso(abs(c2) >= abs(c3))
assumptions % MATLAB, what do you assume about the c variables?
ans = 
a = 1/4 - c2/4 - c3/4 - c1/4;
b = c2/4 - c1/4 + c3/4 + 1/4;
c = c1/4 - c2/4 + c3/4 + 1/4;
d = c1/4 + c2/4 - c3/4 + 1/4;
A = [a;b;c;d]; % Switched to a column vector so we can more easily distinguish the elements
A_asc = sort(A)
A_asc = 
Does this match what you expected?
Obviously, this is not the answer I wanted. The answer I want is
[1/4 - c2/4 - c3/4 - c1/4
c2/4 - c1/4 + c3/4 + 1/4
c1/4 - c2/4 + c3/4 + 1/4
c1/4 + c2/4 - c3/4 + 1/4]
Yes, it looks like it is. [The order of the terms doesn't matter.]
Now for a bigger question: is your expected answer correct in all cases?
delta = A_asc(2:4)-A_asc(1:3)
delta = 
If A_asc is sorted in ascending order, all the elements of A must be greater than 0. Are they? Let's substitute some sample values that satisfy your assumptions for the variables.
% check assumptions
C = [-3, 2, 1.5];
areAssumptionsSatisfied = [abs(C(1)) >= abs(C(2)), abs(C(2)) >= abs(C(3))]
areAssumptionsSatisfied = 1x2 logical array
1 1
subs(delta, [c1, c2, c3], C)
ans = 
That says that the third element of A_asc is smaller than the second. Let's check by looking at the values in A_asc.
subs(A_asc, [c1, c2, c3], C)
ans = 
The third element is indeed smaller than the second. Your expectation about the order in which those terms should be sorted is not correct. But are there parameter values that could make A_asc sorted in ascending order? Yes.
C2 = [4 2 1];
areAssumptionsSatisfied = [abs(C2(1)) >= abs(C2(2)), abs(C2(2)) >= abs(C2(3))]
areAssumptionsSatisfied = 1x2 logical array
1 1
subs(A_asc, [c1, c2, c3], C2)
ans = 
Does it really make sense to ask which element of A is the largest? Sometimes that question doesn't make sense, like the simple example below.
syms x y
isAlways(x > y)
Warning: Unable to prove 'y < x'.
ans = logical
0
  2 件のコメント
jm
jm 2024 年 6 月 19 日
This may still be incorrect. If I change the size relationship of c1, c2, and c3, for example:
syms c1 c2 c3;
assume(c1 >= c3)
assumeAlso (c3 >= c2); %c1>=c3>=c2
a = 1/4 - c2/4 - c3/4 - c1/4;
b = c2/4 - c1/4 + c3/4 + 1/4;
c = c1/4 - c2/4 + c3/4 + 1/4;
d = c1/4 + c2/4 - c3/4 + 1/4; %a<=b<=d<=c
A = [a;b;c;d];
A_asc = sort(A)
the result will still be :
A_asc =
1/4 - c2/4 - c3/4 - c1/4
c2/4 - c1/4 + c3/4 + 1/4
c1/4 - c2/4 + c3/4 + 1/4
c1/4 + c2/4 - c3/4 + 1/4
But in reality, it should be
A_asc =
1/4 - c2/4 - c3/4 - c1/4
c2/4 - c1/4 + c3/4 + 1/4
c1/4 + c2/4 - c3/4 + 1/4
c1/4 - c2/4 + c3/4 + 1/4
Paul
Paul 2024 年 6 月 19 日
Here's the result that "it should be"
sympref('FloatingPointOutput',false);
syms c1 c2 c3;
assume(c1 >= c3)
assumeAlso (c3 >= c2); %c1>=c3>=c2
A_asc =[
1/4 - c2/4 - c3/4 - c1/4
c2/4 - c1/4 + c3/4 + 1/4
c1/4 + c2/4 - c3/4 + 1/4
c1/4 - c2/4 + c3/4 + 1/4]
A_asc = 
If that is truly the correct result, then this difference
A_asc(2) - A_asc(1)
ans = 
should be postive (or perhaps greater than or equal to zero). But nothing in the code disallows c2 and/or c3 from being negative or zero. So c3 = 0 and c2 = -1 satisfies the assumptions and shows that A_sc is, in fact, not the correct result. Perhaps all the c_i variables as supposed to be positive? If that's the case, that assumption can be added as well
assumeAlso([c1 c2 c3],'positive')
assumptions
ans = 
But I don't think that will matter because, as stated previously, sort doesn't sort the elements of A for this problem in accordance with mathemetical rules (at least not as I understand that doc page).

サインインしてコメントする。


jm
jm 2024 年 6 月 19 日
The complete question is to evaluate quantum battery capacity. It comes from
https://journals.aps.org/pra/abstract/10.1103/PhysRevA.109.042424
syms c1 c2 c3 I_2 I_4 sigma_1 sigma_2 sigma_3 sigma_11 sigma_22 sigma_33;
syms e_A e_B;
assume(abs(c1) >= abs(c2));
assumeAlso(abs(c2) >= abs(c3));
assume(e_A >= e_B);
assumeAlso(e_B >= 0);
%defining matrices
I_2 = [1,0;0,1];
sigma_1 = [0,1;1,0];
sigma_2 = [0,-1i;1i,0];
sigma_3 = [1,0;0,-1];
I_4=kron(I_2,I_2);
sigma_11 = kron(sigma_1,sigma_1);
sigma_22 = kron(sigma_2,sigma_2);
sigma_33 = kron(sigma_3,sigma_3);
%defining QState
rho_AB=(1/4).*(I_4+c1.*sigma_11+c2.*sigma_22+c3.*sigma_33)
%evaluating eigenvalues of QState
e_AB = eig(rho_AB)
%defining Hamiltonian
sigma_3_I_2 = kron(sigma_3,I_2);
I_2_sigma_3 = kron(I_2,sigma_3);
H_AB = e_A.*sigma_3_I_2 + e_B.*I_2_sigma_3
rho = rho_AB;
H = H_AB;
seig_rho = sort(simplify(eig(rho)))
n_rho = numel(seig_rho);
seig_H = sort(simplify(eig(H)))
%defining Quantum Bttery Capaity: C_rho_H = \sum_i(e_H(i)*(e_rho(i)-e_rho(n+1-i)))
c = @(x,y) (x.*y);
for i=1:1:n_rho
di_eig(i) = seig_rho(i)-seig_rho(n_rho+1-i);
C(i) = c(seig_H(i),di_eig(i));
end
C_rho_H = sum(C)
C_rho_H = simplify(sum(C))
%The correct answer should be
% C_rho_H = (|c1|+|c2|)(e_A+e_B)+(|c1|-|c2|)(e_A-e_B)

カテゴリ

Help Center および File ExchangeAssumptions についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by