latexify_py icon indicating copy to clipboard operation
latexify_py copied to clipboard

NDArray support

Open odashi opened this issue 3 years ago • 7 comments

Sometimes users want to support NDArrays in this library, e.g., np.array([[a, b], [c, d]]) to

$$\left( \begin{array}{cc}a&b \\ c&d\end{array} \right)$$

This is definitely useful, but often it is not possible to compile:

  • Large arrays.
  • Arrays with more than 2 dimensions.

I think we could start implementing it with reasonable restrictions (e.g., only NDArrays with <= 2 dimensions with small lengths), but it would be also good to keep discussion.

Refs:

  • #38
  • #53
  • #75

odashi avatar Nov 04 '22 17:11 odashi

For 2D arrays, limiting the maximum size to 10 x 10 is perhaps a reasonable starting point. Larger arrays, especially if they are dense, are not great as visual aids even if they are rendered into LaTex.

Casper-Guo avatar Nov 09 '22 19:11 Casper-Guo

For 2D arrays, limiting the maximum size to 10 x 10 is perhaps a reasonable starting point. Larger arrays, especially if they are dense, are not great as visual aids even if they are rendered into LaTex.

I think it's a great idea to constraint the maximum size of matrices. I won't use matrix larger than 10 * 10 in Latex.

moringspeaker avatar Nov 09 '22 23:11 moringspeaker

@Casper-Guo Yes I think it is reasonable. maybe we restrict to show only $(R, 1)$, $(1, C)$ and $(R, C)$ arrays with size $R \le R_{max}$ and $C \le C{max}$, where $R_{max}$ and $C_{max}$ are configs defaulted to some number (10 for example)

odashi avatar Nov 10 '22 01:11 odashi

What should the configuration interface look like? I feel this should be file level rather than function level.

cc: @lakeblair

Casper-Guo avatar Nov 10 '22 17:11 Casper-Guo

@Casper-Guo We can start with hard-coding the constraint into the code until we decided the structure of config files.

odashi avatar Nov 10 '22 19:11 odashi

I was able to implement a basic version of this within src/latexify/function_codegen.py by editing visit_Call

        if func_str == "ndarray":
            # construct matrix
            matrix_str = r"\begin{bmatrix} "
            # iterate over rows
            for row in node.args[0].elts:
                for col in row.elts:
                    matrix_str += self.visit(col) + r" & "
                matrix_str = matrix_str[:-2] + r" \\ "
            matrix_str = matrix_str[:-3] + r"\end{bmatrix}"
            return matrix_str
Screenshot 2022-11-16 at 16 35 54

However, this isn't very extendable especially if we want to support functions like np.eye or np.ones or np.zeroes, so do you have any suggestions on how i can implement this better?

We can also potentially support multiple types of matrix formatting (square brackets vs round brackets vs curly brackets) via a config option?

kshxtij avatar Nov 16 '22 16:11 kshxtij

np.eye, np.ones and other matrix functions will probably be dealt with separately. I think the biggest issue with your current implementation is that it doesn’t generalize to 1D arrays / vectors

If anything, the matrix processing should probably be a separate function instead of resting in, say a visit_x. That will make implementing support for np.inv and np.transpose etc. more straightforward

Casper-Guo avatar Nov 16 '22 17:11 Casper-Guo