# Any way to achieve the L2 norm of a matrix xx is smaller than 1 in JuMP?

**URL:** <https://discourse.julialang.org/t/any-way-to-achieve-the-l2-norm-of-a-matrix-xx-is-smaller-than-1-in-jump/77660>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [March 10, 2022, 12:53am UTC](https://discourse.julialang.org/t/any-way-to-achieve-the-l2-norm-of-a-matrix-xx-is-smaller-than-1-in-jump/77660 "2022-03-10T00:53:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zlq178](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zlq178/32/17952_2.png) [@zlq178](https://discourse.julialang.org/u/zlq178)\
**Post date:** [March 10, 2022, 12:53am UTC](https://discourse.julialang.org/t/any-way-to-achieve-the-l2-norm-of-a-matrix-xx-is-smaller-than-1-in-jump/77660/1 "2022-03-10T00:53:37Z")

</div>

I want to achieve the constraint: norm(A) \<= 1, where A is a matrix variable.

```nohighlight
M = Model(SCS.Optimizer)
@variable(M,A[1:3,1:3])
@constraint(M, [1; A] in SecondOrderCone()) 

```

But it shows error：`@constraint(M, [1; A] in SecondOrderCone())`: Unrecognized constraint building format. Tried to invoke `build_constraint(error, GenericAffExpr{Float64,VariableRef}[1 1 1; A[1,1] A[1,2] A[1,3]; A[2,1] A[2,2] A[2,3]; A[3,1] A[3,2] A[3,3]], SecondOrderCone())`, but no such method exists. This is due to specifying an unrecognized function, constraint set, and/or extra positional/keyword arguments.

If you’re trying to create a JuMP extension, you need to implement `build_constraint` to accomodate these arguments.

How to implement `build_constraint`? Or is any way to achieve the L2 norm of a matrix xx is smaller than 1 in JuMP?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [March 10, 2022, 2:00am UTC](https://discourse.julialang.org/t/any-way-to-achieve-the-l2-norm-of-a-matrix-xx-is-smaller-than-1-in-jump/77660/2 "2022-03-10T02:00:12Z")

</div>

You’re pretty close, you just needed `vec(A)`:

```nohighlight
julia> using JuMP

julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> @variable(model, A[1:3, 1:3])
3×3 Matrix{VariableRef}:
 A[1,1] A[1,2] A[1,3]
 A[2,1] A[2,2] A[2,3]
 A[3,1] A[3,2] A[3,3]

julia> @constraint(model, [1; vec(A)] in SecondOrderCone())
[1, A[1,1], A[2,1], A[3,1], A[1,2], A[2,2], A[3,2], A[1,3], A[2,3], A[3,3]] ∈ MathOptInterface.SecondOrderCone(10)

julia> [1; A]
4×3 Matrix{AffExpr}:
 1 1 1
 A[1,1] A[1,2] A[1,3]
 A[2,1] A[2,2] A[2,3]
 A[3,1] A[3,2] A[3,3]

julia> [1; vec(A)]
10-element Vector{AffExpr}:
 1
 A[1,1]
 A[2,1]
 A[3,1]
 A[1,2]
 A[2,2]
 A[3,2]
 A[1,3]
 A[2,3]
 A[3,3]

```

---

<div class="post-metadata">

**Author:** ![zlq178](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zlq178/32/17952_2.png) [@zlq178](https://discourse.julialang.org/u/zlq178)\
**Post date:** [March 10, 2022, 2:13am UTC](https://discourse.julialang.org/t/any-way-to-achieve-the-l2-norm-of-a-matrix-xx-is-smaller-than-1-in-jump/77660/3 "2022-03-10T02:13:23Z")

</div>

Thanks! It is a surprising way.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [March 10, 2022, 3:22am UTC](https://discourse.julialang.org/t/any-way-to-achieve-the-l2-norm-of-a-matrix-xx-is-smaller-than-1-in-jump/77660/4 "2022-03-10T03:22:41Z")

</div>

> It is a surprising way

Yes. Julia’s array construction syntax can be subtle.

Another option is

```Julia
julia> @constraint(model, [1, A...] in SecondOrderCone())
[1, A[1,1], A[2,1], A[3,1], A[1,2], A[2,2], A[3,2], A[1,3], A[2,3], A[3,3]] ∈ MathOptInterface.SecondOrderCone(10)

julia> [1, A...]
10-element Vector{AffExpr}:
 1
 A[1,1]
 A[2,1]
 A[3,1]
 A[1,2]
 A[2,2]
 A[3,2]
 A[1,3]
 A[2,3]
 A[3,3]

```

Note that none of this is specific to JuMP.

```Julia
julia> A = rand(3, 3)
3×3 Matrix{Float64}:
 0.541972 0.333154 0.674608
 0.946944 0.905994 0.741906
 0.967324 0.304619 0.0828009

julia> [1; A]
4×3 Matrix{Float64}:
 1.0 1.0 1.0
 0.541972 0.333154 0.674608
 0.946944 0.905994 0.741906
 0.967324 0.304619 0.0828009

julia> [1; vec(A)]
10-element Vector{Float64}:
 1.0
 0.5419723001710393
 0.9469443906608599
 0.9673244837937178
 0.3331539326850954
 0.9059938871589952
 0.30461886604324073
 0.6746081111474829
 0.7419057032214258
 0.08280092651279558

julia> [1, A...]
10-element Vector{Float64}:
 1.0
 0.5419723001710393
 0.9469443906608599
 0.9673244837937178
 0.3331539326850954
 0.9059938871589952
 0.30461886604324073
 0.6746081111474829
 0.7419057032214258
 0.08280092651279558

```
