# Coefficients of combination of matrices

**URL:** <https://discourse.julialang.org/t/coefficients-of-combination-of-matrices/98241>\
**Category:** Optimization (Mathematical)\
**Created:** [May 3, 2023, 9:04am UTC](https://discourse.julialang.org/t/coefficients-of-combination-of-matrices/98241 "2023-05-03T09:04:45Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 3, 2023, 2:22pm UTC](https://discourse.julialang.org/t/coefficients-of-combination-of-matrices/98241/6 "2023-05-03T14:22:48Z")

</div>

> [@microlifecc](#):
>
> By equal to we mean that each element of the resultant tensor on the LHS of the equation is almost equal to each element of the tensor D on the right.

In general there may be no solution in which the LHS and RHS are “almost equal”. There will be some errors, which may or may not be small. The question, how do you want the errors to be distributed?

- Do you want them to be as small as possible “on average”? Then probably minimize the L^2 (sum-of-squares) error. This gives you a least-squares (QP) problem.
- Do you want the _biggest individual error_ to be as small as possible? Then probably minimize the L^\infty ([maximum norm](https://en.wikipedia.org/wiki/Chebyshev_distance)). This gives you an LP.

If you’re not sure, I would just try the L^2 norm — least-squares minimization is the most common choice and has the easiest algorithms.

> [@microlifecc](#):
>
> Also is it possible to generalize to more than 3 weights, such as if we had close to 50 tensors on the LHS and we are trying to do a weighted combination of them?

Sure. As @albheim suggested, I would start by [“vectorizing”](https://en.wikipedia.org/wiki/Vectorization_(mathematics)) your tensors into column vectors, since the tensor shape seems to be irrelevant to your problem. Let `d = vec(D)`, and let W be the matrix whose columns are the “vectorized” versions of all of the tensors on the LHS, and flip signs so that all of the terms on the LHS are + (no -). Suppose you have n such tensors, so that you have n weights x and W has n columns. Then you are solving:

\min\_x \Vert d - Wx \Vert \\ \mbox{subject to }\sum\_k x\_k = 1, \; 0 \le x\_j \le 1

which is a convex problem, a constrained least-squares problem in the L2 norm. If you eliminate x\_n by setting x\_n = 1 - \sum\_{k=1}^{n-1} x\_k, then it simplifies to a box-constrained problem:

\min\_\tilde{x} \Vert \tilde{d} - \tilde{W}\tilde{x} \Vert \\ \mbox{subject to }0 \le x\_j \le 1\mbox{ for }j=0,\ldots,n-1

where \tilde{x} is the first n-1 rows of x, \tilde{W} is the first n-1 columns of W minus the last column of W, and \tilde{d} is d minus the last column of W. In the L2 norm, there are lots of approaches to box-constrained least-squares, some of which are linked in the [thread I mentioned above](https://discourse.julialang.org/t/suggestions-needed-for-bound-constrained-least-squares-solver/35611).

---

_[View the full topic](https://discourse.julialang.org/t/coefficients-of-combination-of-matrices/98241)._
