I have a matrix function, which roughly looks like this: $$ Y_{i,j,k} = f(coef, A, B) = coef[i] * A_{i,j} * B_{i,k} $$

def fn(coef, A, B):
    return np.einsum("i,ij,ik->ijk", coef, A, B)

Given, say, following values for coef, A, B:

coef = [1, 4, 16]
A = [
    [.11, .12],
    [.91, .52],
    [.31, .32]
B = [
    [.11, .12, .13],
    [.91, .72, .63],
    [.31, .52, .73]

3d matrix produced by function fn can be visualized like this (I'm just stacking together two slices horizontally, to make it possible to visualize a matrix in 2d):


enter image description here

Given a target (constant) 3d matrix, I need to find values for input parameters A and B such, that they restore element ranks along axis=0.

enter image description here

I was trying to solve this problem minimizing following objective function, using SLSQP implementation provided by scipy.optimize package:

$C=\sum_{j,k} |R_{jk}-\hat R_{jk}|$, where:

$R$ - given target constant matrix ranks
$\hat R$ - actual matrix ranks
$R = argsort(Y) $

def objective_fn(coef, A, B, expected_matrix):
    expected_ranks = expected_matrix.argsort(axis=0)
    actual_ranks = np.einsum("i,ij,ik->ijk", coef, A, B)
    diff = abs(expected_ranks - actual_ranks)
    return diff.sum()

It sort of works.

enter image description here

However, results don't always match expectations. It seems to me, that since objective ranking function is not differentiable, classic optimization using SLSQP may not be an optimal approach here.


  1. Are there any better ways to solve a problem like this?

  2. Is it possible to use matrix decomposition/factorization here so that result factor matrices have specified dimensionality?

  • $\begingroup$ Can you make your question accessible to people not familiar with numpy? $\endgroup$ Commented Nov 28, 2019 at 23:27
  • $\begingroup$ @YuvalFilmus hope it helps. Although not sure what's the right notation for argsort would be. $\endgroup$
    – singleton
    Commented Nov 29, 2019 at 0:44
  • 1
    $\begingroup$ I still see a lot of code. Can you make your question more concise, and do away with code entirely? As for notation, you can use whatever notation you want, as long as you explain it. I have no idea what argsort is supposed to represent. $\endgroup$ Commented Nov 29, 2019 at 8:41


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.