# JuMP中的NLexpression使用问题

using LinearAlgebra
using Distributions

using Random

m = 100
n = 1024

s = 10

# 稀疏信号的构造

xo = zeros(n,1);
pn = randperm(n);
inds = pn[1:s];
xo[inds] = randn(s,1);

# 系数矩阵的构造

A = randn(m,n)
ε = 1e-6
A .-= ε*mean(A, dims = 1); # this apparently is good practice, better conditioning
A ./= sqrt.(sum(abs2, A, dims = 1));

b = A*xo;

using Ipopt

# 构造一个模型对象

BP = Model(Ipopt.Optimizer)

set_optimizer_attribute(BP, “print_level”, 3)

# 设置变量

@variable(BP,bpx[1:n]);

# 设置目标函数

@NLexpression(BP,l1linf,(norm(bpx,1)/norm(x,2)+0.00001))

@NLobjective(BP,Min,l1linf);

# 设置约束条件

@constraint(BP, b .== A*bpx);

# 求解

resBP = JuMP.optimize!(BP)

solution_summary(BP)

# 输出解

BPx = JuMP.value.(bpx)

# 绘制求解的解的图示

BPx = JuMP.value.(bpx)

# 绘制求解的解的图示

psolBP = plot(1:n,BPx,linewidth = 2,label=“BP_Ipopt”,shape=:star5,color=:green)

#### 输出错误信息

@NLexpression表达式的部分：
Warning: Function norm automatically registered with 2 arguments.

│ Calling the function with a different number of arguments will result in an
│ error.

│ While you can safely ignore this warning, we recommend that you manually
│ register the function as follows:
│ ```Julia
│ model = Model()
│ register(model, :norm, 2, norm; autodiff = true)
bpx[1] … bpx[1024] in nonlinear expression. Nonlinear expressions may contain only scalar expressions.