[Updated] Help output algorithms for complex numbers in Pascal Triangle

I got this code example online for a Non-recursive version of Pascal’s Triangle and made modifications to it so that it limits the triangle size to an input n, prints the last line based on the int n=1+ 16 [where +16 = the power to be solved], then outputs the entire binomial expansion format and corresponding numerical and exponential values in the console.

I’d like to adjust the output values of the program to solve based on complex number sets (ex, (a+bi)^2, (a+bi)^3, (a+bi)^4, etc) rather than just real number values of (a+b)^n as it is now.

How could this code be updated so that the output values are representative of complex numbers?

In other words how could this algorithm be included,
When raising complex numbers to a power:
i1 = i, i2 = –1, i3 = –i, and i4 = 1
This pattern repeats into higher powers:
i5 = i , i6 = –1, i7 = – i, and so on

Because powers of the imaginary number i can be simplified, the new solution doesn’t include powers of i, but that of non-powered i’s and negative values.
(with all the Pascal’s Triangle values remaining the same as those without complex numbers, we just need to include the negative values and single i’s into the existing program output values)

//Non-recursive version of Pascal Triangle

//Power of Pasccal's Triangle, +n
int n=1+ 2;

PFont font;
int[][] arr = new int[n][n];
int step=12;
String ll="";
int k=1;
int k1=n-1;
int k2=n-1;
int l=0;
int l1=0;
 
void setup()
{
  size(1600,600);
  smooth();
  font=createFont("Courier New",10);
  textFont(font);
  textAlign(CENTER);
  fill(0);
}
 
void draw(){
  background(220);
  for (int line = 0; line < n; line++)
  {
    ll="";
    for (int i = 0; i <= line; i++)
    {
      if (line == i || i == 0)
           arr[line][i] = 1;
      else
           arr[line][i] = arr[line-1][i-1] + arr[line-1][i];
      ll=ll+str(arr[line][i])+" ";
   
   //Print out only single line, defined by +n
     if (i==n-1 && k<=1){
      println("Example:");
      println("n=2 ; (a+b)^2 = 1 * a^(2)b^(0) + 2 * a^(1)b^(1) + 1 * a^(0)b^(2)");
      println("");
      println("Format:");
      println("n=Input ; (a+b)^n = X * a^(va1)b^(0) + X * a^(va2)b^(vb2) ... + X * a^(0)b^(vb3)");
      println("");
      println("Numerical Coefficients(x) =");
      println(ll);
      k++;
      }
    }
    text(ll,mouseX,mouseY+step*(line+1)-height/2);
  }
      if (l1<1){
      println("-----------------------");
      println("LEFT TO RIGHT");
      println("Exponent Variables(va) =");
      l1++;
      }
    if (k1>0){
     for (int i2 = n-1; i2 >= 0; i2--){ 
     println("a=",i2);
     k1--;
      }
    }
      if (l<1){
      println("-----------------------");
      println("LEFT TO RIGHT");
      println("Exponent Variables(vb) =");
      l++;
      }
    if (k2<n){
     for (int i2 = 0; i2 <= n-1; i2++){ 
     println("b=",i2);
     k2++;
      }
    }
}
1 Like

Okay, I figured out what I need to do, just am not sure how to implement it in the code.

The algorithms for complex number solutions in the Pascal Triangle are as follows -

For two b values the numerical x value of numerical string (ll) is positive, then twice negative, switching back again every two:
b(0) = (+) Numerical Value (x)
b(1) = (+) Numerical Value (x)
b(2) = (−) Numerical Value (x)
b(3) = (−) Numerical Value (x)
b(4) = (+) Numerical Value (x)
b(5) = (+) Numerical Value (x)
b(6) = (−) Numerical Value (x)
b(7) = (−) Numerical Value (x)
b(8) = (+) Numerical Value (x)
b(9) = (+) Numerical Value (x)
b(10) = (−) Numerical Value (x)
b(11) = (−) Numerical Value (x)

For every other b value the x value of numerical string (ll) is real, and then has a single i:
b(0) = Numerical Value (x) is Real (do nothing)
b(1) = Numerical Value (x) is Imaginary (display i)
b(2) = Numerical Value (x) is Real (do nothing)
b(3) = Numerical Value (x) is Imaginary (display i)
b(4) = Numerical Value (x) is Real (do nothing)
b(5) = Numerical Value (x) is Imaginary (display i)
b(6) = Numerical Value (x) is Real (do nothing)
b(7) = Numerical Value (x) is Imaginary (display i)
b(8) = Numerical Value (x) is Real (do nothing)
b(9) = Numerical Value (x) is Imaginary (display i)
b(10) = Numerical Value (x) is Real (do nothing)
b(11) = Numerical Value (x) is Imaginary (display i)

Any thoughts on how I could implement this in the code?

1 Like

This sounds like a perfect application for @quark’s QScript:

http://www.lagers.org.uk/qscript/index.html

Yeah I thought that QScript would be useful but I can’t seem to figure out how to use the operations in even simple code.

for just multiplying two sets of complex numbers I’m stuck at here:
[I was helped with complex number multiplication in another thread (How to multiply (a + bi)(a + bi) with this Complex Number Class?) but it’s not using QScript and trying to use QScript operators with the same method has got me confused.]

import org.qscript.*;
import org.qscript.editor.*;
import org.qscript.errors.*;
import org.qscript.events.*;
import org.qscript.eventsonfire.*;
import org.qscript.operator.*;

Complex z1 = new Complex(2.0, 3.0);
Complex z2 = new Complex(2.0, 3.0);

Complex result = mult(Complex z1, Complex z2);
println(result);

and for the complex numbers to an exponent all I’ve been able to identify in QScript is this, but not how to actually use it:

	/**
	 * Returns the exponential of this number (e^z)
	 */
	public Complex exp() {
		Complex c, d;
		double a, b;
		a = Math.exp(real);
		b = Math.cos(imag);
		c = Z_0_1i.mult(Math.sin(imag));
		d = add(b, c);
		return mult(a, d);
	}

Edit: This method while applicable isn’t the route I’m looking to take, as shown in the following post. (This is the reason I’m moving the thread from the Libraries back to the Coding Questions Section)

1 Like

Since I don’t know enough about QScript yet and know the algorithms required for my solution without the QScript library (as laid out in my second post), I created a new algorithm based on this math example [https://math.stackexchange.com/a/25078] to solve for a single line row in the Pascal’s Triangle. This new algorithm eliminates the need for factoral calculations.

The reason why I wasn’t able to integrate the positive-negative and real-complex algorithms into the non-recursive version of Pascal’s Triangle was that the program utilized strings instead of floats for the array, thus eliminating the ability to make the proper modifications to each consecutive variable like you can with floats.

After working solely with the float calculations I then output the values as int when displayed. The next step is to build a string array from the new Numerical Coefficient (X) int values.
Could someone show an example of that method? That way I can replace the graphical output for the complete triangle, after that I’d consider this solved.

//Non-recursive version of Pascal Triangle for Complex Numbers

//Power of Pasccal's Triangle, +n
int n=1+ 9;

PFont font;
int[][] arr = new int[n][n];
int step=12;
String ll="";
int k1=n-1;
int k2=n-1;
int la=0;
int l1=0;

int k3=0;
int R=1;

// Used for (Real-Imag) Algorithm
float n1 = n-1;
float n2 = 1;
float n3 = 1;
float D = 1;
float zero = 0;

// Used for (+/-) Algorithm
String P="+";
String N="-";
String V=P;
int k=0;
int s=0;
int l=1;
 
void setup()
{
  size(1600,600);
  smooth();
  font=createFont("Courier New",10);
  textFont(font);
  textAlign(CENTER);
  fill(0);
}
 
void draw(){
  background(220);
  
  if (R<2){
      println("Example:");
      println("n=2 ; (a+b)^2 = 1 * a^(2)b^(0) + 2 * a^(1)b^(1) + 1 * a^(0)b^(2)");
      println("");
      println("Format:");
      println("  n=Input ; (a+b)^n = X * a^(va1)b^(0) + X * a^(va2)b^(vb2) ... + X * a^(0)b^(vb3)");
      println("");
      println("LEFT TO RIGHT");
      //println("     Real, Imaginary Algorithm     =     X1r,   X2i,   X3r,  iX4i,   X5r,   X6i...");
      //println("               (+,-) Algorithm     =   (+)X1, (+)X2, (-)X3, (-)X4, (+)X5, (+)X6...");
      //println("");
      
   while (n1>0){
   if (D<2){
    n3 = 1;
    }
    n2 = (n3*n2) * (n1/D);
    
    
    // String Algorithm Based on 1-2, recursive iteration for (+/-)
    // Count (1,2, Reset)
    // Setting String V to new P/N after each reset   
    
    if (l<2){
    k=k+2;
    }else{
    k=k+1;
    }
    l = l + 1;
    
    if (k>4){
    k=1;
    }
    
    if (k==1 || k==2){
    }else{
    s=1;
    }
    if (k==3 || k==4){
    }else{
    s=2;
    }
    
    if (s==1){
    }else{
    V=P;
    }
    if (s==2){
    }else{
    V=N;
    }
      
      if (zero<1){
        println("Numerical Coefficients(X):");
        println("X = ",P,1);
        //println("Numerator = ",1);
        //println("Denominator = ",1);
        //println("");
      }
      zero = 1;
      if ((D/2) == (int)(D/2)){
      println("X = ",V,int(n2));
      }else{
      println("X = ",V,int(n2),'i');
      }
     //println("Numerator = ",int(n1));
     //println("Denominator = ",int(D));
     //println("");
     
     n1 = n1 - 1;
     D = D + 1;
   }
      
  }
   R = 2;
  
  for (int line = 0; line < n; line++)
  {
    ll="";
    for (int i = 0; i <= line; i++)
    {
      if (line == i || i == 0)
           arr[line][i] = 1;
      else
           arr[line][i] = arr[line-1][i-1] + arr[line-1][i];
      ll=ll+str(arr[line][i])+" ";
    }
    text(ll,mouseX,mouseY+step*(line+1)-height/2);
  }
      if (l1<1){
      println("-----------------------");
      println("LEFT TO RIGHT");
      println("Exponent Variables(va) =");
      l1++;
      }
    if (k1>0){
     for (int i2 = n-1; i2 >= 0; i2--){ 
     println("a=",i2);
     k1--;
      }
    }
      if (la<1){
      println("-----------------------");
      println("LEFT TO RIGHT");
      println("Exponent Variables(vb) =");
      la++;
      }
    if (k2<n){
     for (int i2 = 0; i2 <= n-1; i2++){ 
     println("b=",i2);
     k2++;
       }
      }
     
}
1 Like

Update:
I’ve converted the calculated values to strings and output them on the screen, as well as to the console providing single line solutions of the input power of n (Max).
I’d like to clear the string ‘ll’ before each loop is recalculated and output to the screen to display the triangle, rather than all the solutions in a line, but for some reason the implementation isn’t working [failed attempts at implementation not included in the following code].

//Non-recursive version of Pascal Triangle for Complex Numbers

//Power of Pasccal's Triangle, Max = 4
int Base = 1;
int Max = 4;
int n= Base + Max;

PFont font;

//String for Numerical Coefficients
int[][] arr = new int[n][n];
String ll="";

//String for Exponent Variables (va)
int[][] arrA = new int[n][n];
String llA="";

//String for Exponent Variables (vb)
int[][] arrB = new int[n][n];
String llB="";

int step=12;

int k1=n-1;
int k2=n-1;
int la=0;
int l1=0;

int k3=0;
int R=1;

// Used for (Real-Imag) Algorithm
float n1 = n-1;
float n2 = 1;
float n3 = 1;
float D = 1;
float zero = 0;

// Used for (+/-) Algorithm
String P="+";
String N="-";
String V=P;
int k=1;
int s=0;
int l=1;

float N2 = n;

//Used for String Calculation Cycle
float N3 = n;

int Run=1;
 
void setup()
{
  size(1600,600);
  smooth();
  font=createFont("Courier New",10);
  textFont(font);
  textAlign(CENTER);
  fill(0);
}
 
void draw(){
  background(220);
 
  if (Run<2){
      println("Example:");
      println("n=2 ; (a+b)^2 = 1 * a^(2)b^(0) + 2 * a^(1)b^(1) + 1 * a^(0)b^(2)");
      println("");
      println("Format:");
      println("  n=Input ; (a+b)^n = X * a^(va1)b^(0) + X * a^(va2)b^(vb2) ... + X * a^(0)b^(vb3)");
      println("");
      println("LEFT TO RIGHT");
      println("Numerical Coefficients(X):");
      //println("     Real, Imaginary Algorithm     =     X1r,   X2i,   X3r,  iX4i,   X5r,   X6i...");
      //println("               (+,-) Algorithm     =   (+)X1, (+)X2, (-)X3, (-)X4, (+)X5, (+)X6...");
      //println("");
  }
  
  // Cycle through String Calculations and Output until CC>nth power
  for (int CC=0; CC<N3-1; CC++){
    
  if (R<2){
      
   while (n1>0){
   if (D<2){
    n3 = 1;
    }
    n2 = (n3*n2) * (n1/D);
    
    
    // String Algorithm Based on 1-2, recursive iteration for (+/-)
    // Count (1,2, Reset)
    // Setting String V to new P/N after each reset   
    
    if (l<2){
    k=k+2;
    }else{
    k=k+1;
    }
    l = l + 1;
    
    if (k>4){
    k=1;
    }
    
    if (k==1 || k==2){
    }else{
    s=1;
    }
    if (k==3 || k==4){
    }else{
    s=2;
    }
      
      //Prints starting '1' of string on screen and in console
      if (zero<1){
        //println("X = ",P,1);
        ll=ll+int(D)+P+"i";
        //println("Numerator = ",1);
        //println("Denominator = ",D);
        //println("");
      }
    
    if (s>1 && zero>0){
    V=P;
    }
    if (s<2 || zero<1){
    V=N;
    }
      
      //Removes (+) at end of string
      if (n1<2){
      V=" ";
      }
      
      //Prints all values after '1' of string on screen and in console
      if ((D/2) == (int)(D/2)){
      //println("X = ",V,int(n2));
        if (n1<2){
        ll=ll+int(n2)+V+""+"";
         }else{
        ll=ll+int(n2)+V+""+"i";
        }
        if (n2==1 && Run<2){
        print(ll);
         println();
        }
      }else{

      ll=ll+int(n2)+V+"";
      //println("X = ",V,int(n2),'i');
        if (n2==1 && Run<2){
        print(ll);
         println();
        }
      }
      
     //println("Numerator = ",int(n1));
     //println("Denominator = ",int(D));
     //println("");

     zero = 1;
     n1 = n1 - 1;
     D = D + 1;
   }
  }
   R = 2;
   
         text(ll,mouseX,mouseY+step*CC-height/2);
         
         //Resets int and float values for Calculating String Loop 
         n = n-1;
         n1 = n;
         zero=0;
         n1 = n - 1;
         D=1;
         R=0;
         //Outputs Single Line Solution in Console
         Run = 2;
  }
  
      if (l1<1){
      println("-----------------------");
      println("LEFT TO RIGHT");
      println("Exponent Variables(va) =");
      l1++;
      }
    if (k1>0){
     for (int i2 = int(N3)-1; i2 >= 0; i2--){ 
     llA=llA+"a^("+int(i2)+"),";
     if (i2==0){
     println(llA);
     }
     //println("a=",i2);
     k1--;
      }
    }
      if (la<1){
      println("-----------------------");
      println("LEFT TO RIGHT");
      println("Exponent Variables(vb) =");
      la++;
      }
    if (k2<N2){
     for (int i2 = 0; i2 <= N3-1; i2++){ 
     llB=llB+"b^("+int(i2)+"),";
     if (i2==N2-1){
     println(llB);
     }
     //println("b=",i2);
     k2++;
       }
      }

       //text(llA,mouseX,mouseY+step*(2)-height/2);
       //text(llB,mouseX,mouseY+step*(4)-height/2);
         //Outputs All Triangle Solutions in Console
         //Run = 2;

  }

The original code used this for loop to reset the calculation for the string ‘ll’

  for (int line = 0; line < n; line++)
  {
    ll="";

But when attempting to place it in the updated code it results in a blank screen or 0’s, depending on where the for loop is placed.