Graphics, Figures & TablesTikZ: I have to define a variable twice?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

TikZ: I have to define a variable twice?

Post by topsquark »

I'm making a program to fill out a Clebsch-Gordon spin table. There are a few tweaks that still need to be done, but I'm almost there. One of them is this: I have the variables \j1 and \j2. I have to define them in the main block, and then I have to define them again in the function block for coeff. For some reason, if I don't redefine them, they turn into 01 and 02, respectively, instead of their previously defined values.

Here's the code. I have indicated the \j1 and \j2 lines with long arrows on the right.

Code: Select all

\documentclass[tikz]{standalone}
\usepackage{fp}
\usetikzlibrary{math, fixedpointarithmetic}
\begin{document}
\begin{tikzpicture}[fixed point arithmetic]
% Variables
%    j1, m1, j2, m2: Input spin vectors
%    jtot, mtot: Input total spin vector
%    jitot, mitot, mi1, mi2: Temporary spin vector values for the coeff function
%    blocks: Number of coefficient boxes
%    dw, dh: Number of cells per per box
%    aoldx, aoldy: Coordinates of the top right of the last coeefficient grid
%    anewx, anewy: Coordinates of the top right of the new coefficient grid
%    num: Number to be converted to fraction
%    r: Returned fraction
%    w, h: Width and height of one cell
%    n, d: Throw-away integers
%    c, i, j, k, hm1, p, q, s, wjtot: Throw-away variables
% Program Block
     \tikzmath {
% Convert A Decimal To A Fraction
         function f(\num) {
               integer \n, \d;
               \stop=0;
               for \k in {1,...,200} {
                    \a=\k*\num;
                    if \stop==0 then {
                         if int(abs(\a))==abs(\a) then {
                              if \k==1 then {\n=\num; let \r="\n"; \stop=1; }
                                   else {\n=\a; \d=\k; let \r="\n/\d"; \stop=1; };
                         };
                    };
              };
              return \r;
          };
          function coeff(\jitot,\mitot,\mi1,\mi2) {
               \j1=1/2; \j2=1/2; %<--------------------------------------------------------------------
               \s=0;
               for \ki in {0,...,0} {
                    \s=\s+1/((\ki)!*(\j1+\j2-\jitot-\ki)!*(\j1-\mi1-\ki)!*(\j2+\mi2-\ki)!*(\jitot-\j2+\mi1+\ki)!*(\jitot-\j1-\mi2+\ki)!);
               };
               \c=sqrt((2*\jitot+1)*(\jitot+\j1-\j2)!*(\jitot-\j1+\j2)!*(\j1+\j2-\jitot)!/(\j1+\j2+\jitot+1)!);
               \c=\c*sqrt((\jitot+\mitot)!*(\jitot-\mitot)!*(\j1-\mi1)!*(\j1+\mi1)!*(\j2-\mi2)!*(\j2+\mi2)!);
               \c=\c*\s;
               return \c;
          };
          \w=1.2; \h=0.6;
          \j1=1/2; \j2=1/2; %<-------------------------------------------------------------------------
          \m1=\j1; \m2=\j2;
          \jtot=\j1+\j2; \mtot=\jtot;
          \blocks=2*(\j1+\j2)+1;
% Spins
          \aoldx=0; \aoldy=0;
          \p=f(\j1); \q=f(\j2); {\node at ({\aoldx-\w},{\aoldy+\h/2}) {\p + \q}; };
          for \i in {1,...,\blocks} {
% J Block Settings
               \wjtot=\jtot; \c=0; \stop=0;
               for \x in {0,...,2*\jtot} {
                    if \stop==0 then {
                         if \wjtot>=\mtot then {\c=\c+1; };
                         if \wjtot<=-\mtot then {\stop=1; };
                         \wjtot=\wjtot-1;
                    };
               };
               \dw=\c;
% m1, m2 Block Settings
               \hm1=\m1; \c=0;
               for \x in {0,...,2*\m1} {
                    if -\m2>\mtot-(\m1-\x) then {\hm1=\hm1-1; };
                    if \m2<\mtot-(\m1-\x) then {\c=\c+1; };
               };
               \dh=(2*\m1+1)-(\m1-\hm1)-\c;
               if \i==1 then {\dw=1; \dh=1; \hm1=\m1; };
               if \i==\blocks then {\dw=1; \dh=1; \hm1=-\m1; };
% Boxes and Spin States
               \anewx=\aoldx+\dw*\w; \anewy=\aoldy-\dh*\h;
               {\draw (\aoldx,\aoldy) rectangle (\anewx,\anewy); };
               {\draw[line width=2pt] (\aoldx,\aoldy) -- ++ ({-2*\w},0) -- ++(0,-\dh*\h) -- ++ ({2*\w+\dw*\w},0) -- ++(0,{2*\h+\dh*\h}) -- ++(-\dw*\w,0) -- cycle; };
               for \j in {0,...,\dw-1} {
                    for \k in {0,...,\dh-1} {
                         \p=f(\jtot-\j); \q=f(\mtot); {\node at ({\aoldx+\w/2+\j*\w},{\aoldy+3*\h/2}) {\p}; \node at ({\aoldx+\w/2+\j*\w},{\aoldy+\h/2}) {\q}; };
                         \p=f(\hm1-\k); \q=f(\mtot-(\hm1-\k)); {\node at ({\aoldx-3*\w/2},{\aoldy-\h/2-\k*\h}) {\p}; \node at ({\aoldx-\w/2},{\aoldy-\h/2-\k*\h}) {\q}; };
                         \p=coeff(\jtot-\j,\mtot,\hm1-\k,\mtot-(\hm1-\k));
                         \q=f(round(100*(\p)^2)/100); { \node at ({\aoldx+\w/2+\j*\w},{\aoldy-\h/2-\k*\h}) {\q}; };
                    };
               };
               \mtot=\mtot-1;
               \aoldx=\anewx; \aoldy=\anewy;              
          };
    };
\end{tikzpicture}
\end{document}
Thanks!

-Dan

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

TikZ: I have to define a variable twice?

Post by topsquark »

Okay, I figured it out. When I was testing things out I had shortened my loops to go from 0 to 0. What was happening was, inside the loop \j in {0,...,0}, I was getting \j1 = 01 because \j =0. I changed my for loop variable to \t and now I don't have to redefine anything.

I realize that TikZ isn't a strictly Mathematically driven programming language but some of this is just killing me!

-Dan
Post Reply