How do I use CVXPY with a trace constraint? - cvxpy

I would like to set the program in CVX where the optimized variable is a square matrix A, with a constraint on its trace having to be larger than a certain value. I know how to setup everything, based on these examples, except for adding a constraint on the trace of A. How do I impose an inequality constraint on the trace?

Related

Determining which of two layout constraints with different priorities is determining the value of the dimension/anchor they affect

Say I have two NSLayoutConstraints with different priorities that would both affect the height of some view (middleView).
Below, middleView is pinned to the bottom of topView and its height is 500, unless the top of bottomView would force it to be smaller.
// topView and bottomView have well-defined constraints, and middleView has well defined x-axis/width constraints not shown here
middleView.topAnchor.constraint(isEqual: topView.bottomAnchor).isActive = true
let heightConstraint = middleView.heightAnchor.constraint(isEqualToConstant: 500)
heightConstraint.priority = UILayoutPriority.high
heightConstraint.isActive = true
let bottomConstraint = middView.bottomAnchor.constraint(isLessThanOrEqualTo: bottomView.topAnchor, constant: someMargin)
bottomConstraint.isActive = true // priority is .required by default
Besides just checking if the height of my view is 500, is there some other way to determine which of these two constraints is "in effect"? Say bottomView is in such a position that if forces middleView's height to be less than 500, does heightConstraint have some property to determine that it has been bypassed by a constraint with a greater priority than its own?
I would like to use heightConstraint as some sort of 'switch' that triggers something when it is satisfied.
This can't be answered because both may have some effect. Even when a non-rquired constraint cannot be satisfied, it can influence layout. From the Auto Layout Guide:
Even if an optional constraint cannot be satisfied, it can still influence the layout. If there is any ambiguity in the layout after skipping the constraint, the system selects the solution that comes closest to the constraint. In this way, unsatisfied optional constraints act as a force pulling views towards them.
Auto layout uses a constraint solver to calculate its values. (I believe it's still using Cassowary, or something close to it.) Constraint solvers are basically linear algebra engines. You put a bunch of linear equations into a matrix and you try to solve it for some vector that makes everything "best." One trade-off of that approach is that everything is solved together, and it's not always obvious why a particular value was chosen. It's what it is because it's part of the total solution. (In this way, CSP systems are very similar to machine learning systems. They give answers that are "correct," but they don't always provide a step-by-step of their reasoning they way branching-if logic can. It's why they can be so challenging to debug.)
To your question, I would base your logic off the thing you actually care about. "A constraint fired" is never the actual thing you cared about (especially since constraints don't "fire"). You generally care about something in the result (such as the final hight), so you should check that.
Looks like it's not possible. The alternative would be to compare the dimension/anchor constant in question to the possible values.

Why is this combination of constraints ambiguous?

XCode 7
Do you know why this is being considered ambiguous?
I defined that I wanted it centered horizontally and vertically... nothing should be questionable there... then I defined that I wanted it 650 points or larger in horizontal and vertical... so why the ambiguity?
** EDIT **
I'm following up with some more images based on a comment below.
Notice that ambiguity appears to be resolved, yet I get no output on the screen which should be filled edge to edge with a map view.
You chose an inequality constraint of greater than or equal to. Since the size could change, (be greater than 650 height or width) it is considered ambiguous. If you set it equal to 650, it should no longer be considered ambiguous.

How do I make a constraint "5-53" space between views?

when I want to make a constraint saying that the layout system can have minimum 5 and maximum 53 points between two views, such as
ViewA.Leading is greater or equal ViewB.Trailing 5 (pri 1000 multiplier 1)
ViewA.Leading is less or equal ViewB.Trailing 53 (pri 1000 multiplier 1)
Interface Builder always gets upset with me, giving me the "Inequality Constraint Ambiguity" error. If this is not the way to make such a constraint, how should it be instead?
(please, I insist on expressing the constraints in a storyboard through Interface Builder)
Cheers
Nik
The ambiguity is that the auto layout system doesn't know exactly how much space to put. You have successfully placed limits on the range of allowed space, but there are still any number of solutions. For example, 20 points works, but so does 40, and so does 31.2875.
The system needs additional information to pick a specific distance. How much space, specifically, would you prefer if all other constraints allow flexibility?
You could, for example, set a constraint ViewA.Leading equal to ViewB.Trailing plus 20 but at a lower priority. The lower priority would allow it to be overridden by higher priority constraints and things such as content hugging or compression resistance priority. But, all else being equal, the system will try for 20 or as close to 20 as possible. That removes the ambiguity.
But, ultimately, you need to decide how the system should resolve things when there are remaining degrees of freedom and give the system the corresponding constraints to eliminate that freedom, so it can come up with one right answer.

Solid solution to flexibly resize UIView based on subviews

Imagine you have four or so views, all width 100, different heights. You have a wrapper view W which holds them all.
A |
B | W
C |
D |
the heights of the small views can change. At that time you want them all to move, float, appropriately, and resize W.
Now, I was just about to write a few lines of code to do this.
So .. (1) you'd have W find all the subviews and list them in order from top to bottom. Then (2) each time there is a change, you'd (3) reposition each of ABCD. the position of each one is the sum of the heights of the items above it, and (4) resize W to the sum of all heights.
Now that's all fine but -- idiots reinvent the wheel!
Am I missing something obvious in iOS? is there already a package everyone uses to do this all the time? Or something built in? What's the situation?
(Note that of course frustratingly, for our Android friends this is built in! And of course any web-html system does this automatically.)
What's the right engineering solution for iOS views here? For the record this is iOS7+ only, no old-fashioned stuffs need be covered, if it makes a difference. Cheers
(1) you'd have W find all the subviews and list them in order from top
to bottom. Then (2) each time there is a change, you'd (3) reposition
each of ABCD. the position of each one is the sum of the heights of
the items above it, and (4) resize W to the sum of all heights.
You can use constraints in Interface Builder for that whole process, no code required. Do this:
set the width of subview A to 100
constrain B, C, and D to match A's width
add vertical spacing constraints between A and B, B and C, and C and D to maintain their relative position
add a vertical spacing constraint between W (the superview, shown in gray) and A
add a vertical spacing constraint between W and D
add leading and trailing space constraints between W and view A
You'll end up with something that looks like this:
The constraints editor in Xcode isn't completely intuitive, but it is easy to use once you understand what you can and can't do with constraints in IB and when you need to use code to set up the constraints.

How to handle text nodes that contain text that goes below baseline?

I have the following problem with Tikz/Latex:
I have some nodes that contain text. Most text doesn't have letters that reach below the baseline. But for those nodes where it happens, the ratio and placement of node height and text/baseline is off. Have a look at the example image.
Solutions I know of:
make the nodes' minimum height bigger. This results in the nodes being sized the same, but the baseline is still at different heights
use struts. This results in all nodes being laid out like the ones containing the offending text. This results in too much space between the baseline of the text and the surrounding box for most nodes
There's a property for nodes: text depth
One can simply set it to 0pt for the offending (or all) nodes.
Note
Of course this is well documented in the pgfmanual.
This is more of a pointer (can't get TikZ at the moment), but since you are not getting anything else, there are two things to try:
1) There is the $\smash[b]{\text{my text y}}$ (you need amsmath for this).
2) Maybe \raisebox{0pt}[0pt]{my text y} is what you want.

Resources