# Optimize the product of a matrix and scalar

**URL:** <https://discourse.julialang.org/t/optimize-the-product-of-a-matrix-and-scalar/92443>\
**Category:** General Usage\
**Tags:** optim\
**Created:** [January 3, 2023, 5:35am UTC](https://discourse.julialang.org/t/optimize-the-product-of-a-matrix-and-scalar/92443 "2023-01-03T05:35:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ejlongman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ejlongman/32/36801_2.png) [@ejlongman](https://discourse.julialang.org/u/ejlongman)\
**Post date:** [January 3, 2023, 5:35am UTC](https://discourse.julialang.org/t/optimize-the-product-of-a-matrix-and-scalar/92443/1 "2023-01-03T05:35:22Z")

</div>

This code:

```julia
function objective(xy)
    x, y = xy
    return (3y^2*x + x^3-m3)*W*(3y^2*x + x^3-m3)
end

xy0 = [x, y]
result = optimize(objective, xy0, NelderMead())
println(result)

minimizer = result.minimizer
x, y = minimizer
println("x = ", x)
println("y = ", y)

```

where W=:

```julia
2×2 Matrix{Float64}:
 2.87989e-6 9.85217e-7
 9.85217e-7 4.4135e-7

```

Returns the error:

```julia
MethodError: Cannot `convert` an object of type Matrix{Float64} to an object of type Float64
Closest candidates are:
  convert(::Type{T}, !Matched::Base.TwicePrecision) where T<:Number at twiceprecision.jl:273
  convert(::Type{T}, !Matched::AbstractChar) where T<:Number at char.jl:185
  convert(::Type{T}, !Matched::CartesianIndex{1}) where T<:Number at multidimensional.jl:130
  ...

```

I seem to be able to multiply a scalar by a matrix by a scalar normally:

```julia
5*W*5

```

returns:

```julia
2×2 Matrix{Float64}:
 7.19973e-5 2.46304e-5
 2.46304e-5 1.10338e-5

```

Does anyone know why it does not work within the Optim environment, and how to fix it? Thank you!

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [January 3, 2023, 6:10am UTC](https://discourse.julialang.org/t/optimize-the-product-of-a-matrix-and-scalar/92443/2 "2023-01-03T06:10:26Z")

</div>

The `objective` function returns a matrix when it should return a scalar.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [January 3, 2023, 7:01am UTC](https://discourse.julialang.org/t/optimize-the-product-of-a-matrix-and-scalar/92443/3 "2023-01-03T07:01:15Z")

</div>

If `5*W*5` truly is what you would like to do, you might save some work by instead computing `5^2*W`. But since you are treating the result as an objective function, you likely intend to compute an inner product?

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [January 3, 2023, 11:17am UTC](https://discourse.julialang.org/t/optimize-the-product-of-a-matrix-and-scalar/92443/4 "2023-01-03T11:17:26Z")

</div>

Besides being lazy with `W` in the following, I needed also a value for `m3`, but this for example works (assuming your terms with + are vectors

```julia
using Optim
	m3 = 0.3
	function objective(xy)
    	x, y = xy
    	return [3y^2*x, x^3-m3]'*W*[3y^2*x, x^3-m3]
	end
	W = [1.0 1.0; 0.0 1.0]
	x = 0.0
	y = 0.0
	xy0 = [x, y]
	result = optimize(objective, xy0, NelderMead())
	println(result)

	minimizer = result.minimizer
	x, y = minimizer
	println("x = ", x)
	println("y = ", y)

```

returns

```julia
 * Status: success

 * Candidate solution
    Final objective value: 1.283678e-09

 * Found with
    Algorithm: Nelder-Mead

 * Convergence measures
    √(Σ(yᵢ-ȳ)²)/n ≤ 1.0e-08

 * Work counters
    Seconds run: 0 (vs limit Inf)
    Iterations: 46
    f(x) calls: 92

x = 0.6694227744863399
y = -0.004499203729393773

```
