# Difference between "x.^2", "power(x,2)", and "square(x)"

Hi all,

I have a question about the CVX grammar. Assuming that “x” is a variable vector, or a affine expression. Now I want to square each element of “x”, and I think there are 3 kind of expressions (or more?) to do this:
1: x.^2
2: power(x,2)
3: square(x)

Does these 3 expressions correct in CVX grammar (conforming to the DCP rules )? (If they are not correct, then what expression is correct?)
Does these expressions have difference on accuracy of result and computation time, when running the CVX solvers?

In short, my question is what is the best expressions to realize “square each element of x”.

Thx!
Yvan

1 Like

I don’t think there’s any difference. Use whichever of them you want.

Thanks. Actually, what makes me confused is Section 5.7 of CVX users’ guide, which says " (x^2 + 1)^2 = x^4 + 2x^2 + 1 is convex. But CVX will reject the expression, because the outer square cannot accept a convex argument.". So CVX users’ guide recommends to use expression “square_pos( square( x ) + 1 )”.

In my opinion, using the CVX-defined function such as “square_pos” and “square” instead of original MATLAB operator such as “^2” and “.^2” solves the problem mentioned above, because those CVX-defined function may do some translation work to make sure the underlying solver works successfully.

Considering the presence of those translation work, I worry that different expressions such as “x.^2”, “power(x,2)”, and “square(x)” may make the underlying solver works in different way. And this may cause difference on accuracy of result and computation time?

I know my worry is probable nothing, but it really bother me. Hope you can help me. Thanks.

1 Like

Your opinion is not fully correct. square_pos is different than `square’ and the other forms you provided. I stand by my previous reply.

CVX 3.0beta does accept the expression mentioned in the 2nd sentence of your first paragraph. Unfortunately, CVX 3.0beta is riddled with bugs, so I recommend you don’t use it. Rather, use CVX 2.1 with square_pos as needed.

You mean the following version of a CVX will accept expression like "(x^2 + 1)^2 " ?

CVX 3.0beta will accept that. However, due to bugs in CVX 3.0beta, I recommend you use square_pos with CVX 2.1 instead.

Thank you very much!