Take the vector of unknowns , and apply some generic differentiable function . The Jacobian is then given by a matrix such that:
For example, suppose
n=2. Then (using 0-based indexing)
The Jacobian of
f is then
The goal of this challenge is to print this Jacobian matrix.
Your program/function should take as input two positive integers
n, which represent the number of components of
u respectively. The input may come from any source desired (stdio, function parameter, etc.). You may dictate the order in which these are received, and this must be consistent for any input to your answer (please specify in your answer).
Something which represents the Jacobian matrix. This representation must explicitly spell out all elements of the Jacobian matrix, but the exact form of each term is implementation defined so long as is unambiguous what is being differentiated and with respect to what, and every entry is outputted in a logical order. Example acceptable forms for representing a matrix:
- A list of lists where each entry of the outer list corresponds to a row of the Jacobian, and each entry of the inner list corresponds to a column of the Jacobian.
- A string or textual output where each line is a row of the Jacobian, and each delimiter separated entry in a line corresponds to a column of the jacobian.
- Some graphical/visual representation of a matrix. Example: what is shown by Mathematica when using the
- Some other dense matrix object where every entry is already stored in memory and can be queried (i.e. you can't use a generator object). Example would be how Mathematica internally represents a Matrix object
Example entry formats:
- A string of the form
d f_i/d u_j, where
jare integers. Ex:
d f_1/d u_2. Note that these spaces between
x_2are optional. Additionally, the underscores are also optional.
- A string of the form
d f_i(u_1,...,u_n)/d u_jor
d f_i(u)/d u_j. That is, the input parameters of the function component
f_iare optional, and can either be explicitly spelled out or left in compact form.
- A formatted graphical output. Ex.: what Mathematica prints when you evaluate the expression
You may choose what the starting index for
f are (please specify in your answer). The output may be to any sink desired (stdio, return value, output parameter, etc.).
The following test cases use the convention
m,n. Indexes are shown 0-based.
1,1 [[d f0/d u0]] 2,1 [[d f0/d u0], [d f1/d u0]] 2 2 [[d f0/d u0, d f0/d u1], [d f1/d u0, d f1/d u1]] 1,2 [[d f0/d u0, d f0/d u1]] 3,3 [[d f0/d u0, d f0/d u1, d f0/d u2], [d f1/d u0, d f1/d u1, d f1/d u2], [d f2/d u0, d f2/d u1, d f2/d u2]]
This is code golf; shortest code in bytes wins. Standard loopholes are forbidden. You are allowed to use any built-ins desired.