Take the vector of unknowns enter image description here, and apply some generic differentiable function enter image description here. The Jacobian is then given by a matrix enter image description here such that:

enter image description here

For example, suppose m=3 and n=2. Then (using 0-based indexing)

enter image description here

enter image description here

The Jacobian of f is then

enter image description here

The goal of this challenge is to print this Jacobian matrix.


Your program/function should take as input two positive integers m and n, which represent the number of components of f and 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:

  1. 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.
  2. 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.
  3. Some graphical/visual representation of a matrix. Example: what is shown by Mathematica when using the MatrixForm command
  4. 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:

  1. A string of the form d f_i/d u_j, where i and j are integers. Ex: d f_1/d u_2. Note that these spaces between d and f_1 or x_2 are optional. Additionally, the underscores are also optional.
  2. A string of the form d f_i(u_1,...,u_n)/d u_j or d f_i(u)/d u_j. That is, the input parameters of the function component f_i are optional, and can either be explicitly spelled out or left in compact form.
  3. A formatted graphical output. Ex.: what Mathematica prints when you evaluate the expression D[f_1[u_,u_2,...,u_n],u_1]

You may choose what the starting index for u and f are (please specify in your answer). The output may be to any sink desired (stdio, return value, output parameter, etc.).

Test cases

The following test cases use the convention m,n. Indexes are shown 0-based.

[[d f0/d u0]]

[[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]]

[[d f0/d u0, d f0/d u1]]

[[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.


7 Answers 7


Python, 63 bytes

lambda m,n:["df%d/du%%d "%i*n%tuple(range(n))for i in range(m)]

For m=3,n=2, outputs

['df0/du0 df0/du1 ', 'df1/du0 df1/du1 ', 'df2/du0 df2/du1 ']

The string formatting is 1 byte shorter than the more obvious

lambda m,n:[["df%d/du"%i+`j`for j in range(n)]for i in range(m)]

R, 93 78 bytes

function(M,N){v=vector();for(i in 1:N){v=cbind(v,paste0("df",1:M,"/du",i))};v}

The numbering is 1-based.

-15 bytes thanks to @AlexA. remaks !

  • 1
    \$\begingroup\$ You can save a few bytes by removing the function name, i.e. f=, as that's common practice here. R also returns the last thing evaluated in a function, so you can just use v instead of return(v). \$\endgroup\$
    – Alex A.
    Commented Aug 3, 2016 at 15:35
  • 1
    \$\begingroup\$ You should also be able to save bytes by indexing from 1 instead of 0, which is permitted by the OP. \$\endgroup\$
    – Alex A.
    Commented Aug 3, 2016 at 15:38
  • \$\begingroup\$ @AlexA. Very interesting remarks, thanks a lot ! \$\endgroup\$
    – Frédéric
    Commented Aug 3, 2016 at 16:25
  • \$\begingroup\$ My pleasure. :) \$\endgroup\$
    – Alex A.
    Commented Aug 4, 2016 at 16:30

Maxima, 68 bytes

It's too bad I don't know Maxima like I know my dear C and Matlab. But I'll try nontheless.


Example session using TeXmacs as a Maxima interpreter, mostly for neat math rendering:

Maxima session in TeXmacs

There may very well be better ways doing lists and such in Maxima (I would especially like to make the functions appear without the list markers, []), but I don't know the language well enough.


Ruby, 53 bytes

f is 0-indexed, u is 1-indexed. Try it online!

->m,n{m.times{|i|p (1..n).map{|j|"d f#{i}/d u#{j}"}}}

Each row occupies one line as an array representation as seen below. If it isn't compliant to spec, please let me know and I will fix it.

["d f0/d u1", "d f0/d u2", "d f0/d u3"]
["d f1/d u1", "d f1/d u2", "d f1/d u3"]

Cheddar, 79 49 bytes


Apparently a fork of this answer.

For 3,2 returns:

[["df0/du0", "df0/du1", "df0/du2"], ["df1/du0", "df1/du1", "df1/du2"]]

Jelly, 18 bytes

ps⁴’“ df“/du”ż$€€G

Try it out!

Given (m, n) = (3, 2), prints (with spaces marked as ·:)


C, 125 bytes:

main(w,y,b,q){scanf("%d %d",&w,&y);for(b=0;b<w;b++){for(q=0;q<y;q++)printf("d f%d/du u%d%s",b,q,q<y-1?", ":"");printf("\n");}}

Takes input as 2 space separated integers, b y, and outputs the Jacobian Matrix as y comma separated strings on b lines.

C It Online! (Ideone) or Test Suite (Ideone)


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.