5
$\begingroup$

I am trying to make a Table of expressions Defer[Sqrt[3i] + 2 Sqrt[3j]], e.g

Defer[Sqrt[27 ] + 2 Sqrt[108]] == Sqrt[27 ] + 2 Sqrt[108]

With Table, I tried

TableForm@
 Activate@
  Table[Inactivate@Defer[Sqrt[3 i] + 2 Sqrt[3 j]] == 
    Sqrt[3 i] + 2 Sqrt[3 j] , {i, 1, 20}, {j, 21, 40}]

How can I tell Mathematica Out put in the form

Sqrt[27] + 2 Sqrt[108] == 3 Sqrt[3] + 12 Sqrt[3] == 15 Sqrt[3]
$\endgroup$

2 Answers 2

6
$\begingroup$
func = Defer[Sqrt[#] + 2 Sqrt[#2]] == (Defer[# + #2] &)[Sqrt[#], 2 Sqrt[#2]] == 
    Sqrt[#] + 2 Sqrt[#2] &;

Table[func[3 i, 3 j], {i, 20}, {j, 21, 40}] // TableForm

Just keep in mind that Defer is nothing but a function with HoldAll attribute that will disappear when being output in notebook (to be more precise, when it passes through MakeBoxes stage).

$\endgroup$
4
  • $\begingroup$ How can I remove when 3 i and 3j get the same value? $\endgroup$ Aug 6, 2023 at 14:41
  • $\begingroup$ @minhthien_2016 The simplest solution I can think of at the moment is using Nothing e.g. Table[If[i == j, Nothing, func[3 i, 3 j]], {i, 20}, {j, 20}] $\endgroup$
    – xzczd
    Aug 7, 2023 at 0:05
  • $\begingroup$ @xzczd Can I format like this Sqrt[27] + 2 Sqrt[108] == Sqrt[3*3^2] + 2 Sqrt[3*6^2] = 3 Sqrt[3] + 12 Sqrt[3] == 15 Sqrt[3]? $\endgroup$
    – Laurenso
    Aug 7, 2023 at 1:07
  • $\begingroup$ @Laurenso It's possible, but more advanced, because evaluation like 3×3^2 doesn't happen internally, so you need something more than evaluation control: trans = Sqrt[#] /. {a_. Sqrt[b_] :> Sequence[b, a], a_ :> Sequence[1, a]} &; template = Defer[Sqrt[# #2^2] + 2 Sqrt[#3 #4^2]] &; func2 = template[trans[#], trans[#2]] &; func2[27, 108] $\endgroup$
    – xzczd
    Aug 7, 2023 at 2:24
2
$\begingroup$
t0 = Table[
    Inactivate[Sqrt[3 i] + 2 Sqrt[3 j]], {i, 1, 20}, {j, 21, 40}] /. 
   Inactive[Sqrt] :> Sqrt;

t1 = Table[
    Inactivate[Sqrt[3 i] + 2 Sqrt[3 j]], {i, 1, 20}, {j, 21, 40}] /. 
   Inactive[Times] :> Times;

t2 = Table[Sqrt[3 i] + 2 Sqrt[3 j], {i, 1, 20}, {j, 21, 40}];

(MapThread[List, {t0, t1, t2}, 2] // 
    Replace[#, {a_, b_, c_} :> a == b == c, 2] &) /. 
  Inactive[Sqrt][a_] :> Sqrt[ToString@a] // TableForm

A sample of the output looks like:

enter image description here

$\endgroup$
2
  • 2
    $\begingroup$ A bit different from OP's desired output, I'm afraid. In t0, Plus and Sqrt should be Inactivated, I think. $\endgroup$
    – xzczd
    Aug 6, 2023 at 12:05
  • $\begingroup$ Thanks for spotting. I wanted to retain the i and j so it is a bit different. $\endgroup$
    – Syed
    Aug 6, 2023 at 14:53

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.