4
$\begingroup$

How can I code a program to display, for example, $(a+b) ^ 3$ or $(a + b + c) ^ 3$ or $(a+b)^2$ or other variations of along the same lines.

A picture is worth more than 1000 words.

Here is a picture of $(a+b)^2$.

example 1

And here is one of $(a+b)^3$.

example 1

$\endgroup$
0

1 Answer 1

3
$\begingroup$

This is not the most elegant code I've written but it's a fair start I think.

draw[HoldPattern[(+a__)^2]] := Module[{f1, n = Length[{a}]}, f1[i_] := {i - 1, i + 1} i/2; f1[i_, j_] := (f1 /@ {i, j})\[Transpose]; Graphics[{ EdgeForm[{Thick, Black}], Array[{RandomColor[], Rectangle @@ f1[##]} &, {n, n}], Array[Text[(1 a)[[{##}]], Mean @ f1[##]] &, {n, n}], Array[Text[(1 a)[[#]], {-1/4, Mean @ f1[#]}] &, n], Array[Text[(1 a)[[#]], {Mean @ f1[#], -1/4}] &, n] } ] ] draw[(a + b)^2] draw[(a + b + c)^2] draw[(a + b + c + d)^2] 

enter image description here

enter image description here

enter image description here

  • I just used RandomColor for the colors as I was not sure the the actual colors you wanted; if you clarify that I'll try to implement it.

  • This is only for the 2D case but similar methods could be applied to the cubic case as well.

  • There is redundancy in this code but I felt that this form may be more readable than e.g. attempting to compress four Array calls into two.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.