C++ help

tacosareveryyummy

Supreme [H]ardness
Joined
Jul 25, 2005
Messages
5,300
Just to clarify, this is in no way connected to any class or graded assignment. I am writing a program that will enable me to numerically compute color ordered spinor helicity amplitudes efficiently using the spinor helicity formulism.

My problem is thus. I define 4 separate 4 vectors for use in the evaluation of several expressions. Within each expression there are square roots. The values inside the roots can take on negative values (based on the assigned vectors) and cause the square roots to be imaginary. I am not sure how to implement this into the program. The program must also work for roots that are not imaginary. My head hurts. It's probably very simple I just need a break.

Any help would be appreciated.

Code:
//06/15/09
//This code will build the helicity amplitudes from the provided 4-vectors

#include<iostream>
#include<cmath>
#include<iomanip>
#include<complex>

using namespace std;

int i = sqrt(-1);
char choice;
 
class vectors
{
public:

  int vect1, vect2;
  double p01,px1,py1,pz1,p02,px2,py2,pz2;
  

  vectors();
  void input();
  double squarebracket();
  double anglebracket();
  double roundbracket();

};

vectors::vectors()
{
}

void vectors::input()
  {

    cout << "Specify the vectors you wish to use. Enter choices 1-4" << endl;
    cout << "Enter vector one." << endl; 
    cin >> vect1; 
    cout << "Enter vector two." << endl;
    cin >> vect2;

  if( vect1 = 1)
    {
      p01 = -1;
      px1 = 0;
      py1 = 0;
      pz1 = 1;
    }

  else if( vect1 = 2)
    {
      p01 = -1;
      px1 = 0;
      py1 = 0;
      pz1 = 1;
    }
 
  else if( vect1 = 3)
    {
      p01 = 1;
      px1 = 0.5;
      py1 = 0;
      pz1 = 0.5;
    }
  else if( vect1 = 4)
    {
      p01 = 1;
      px1 = -0.5;
      py1 = 0;
      pz1 = -0.5;
    }
  else if( vect2 = 1)
    {
      p02 = -1;
      px2 = 0;
      py2 = 0;
      pz2 = 1;
    }
  else if( vect2 = 2)
    {
      p02 = -1;
      px2 = 0;
      py2 = 0;
      pz2 = 1;
    }
  else if( vect2 = 3)
    {
      p02 = 1;
      px2 = 0.5;
      py2 = 0;
      pz2 = 0.5;
    }
  else ( vect2 = 4);
    {
      p02 = 1;
      px2 = -0.5;
      py2 = 0;
      pz2 = -0.5;
    }

    cout << "Specify whether you want [], <>, or (). Enter A for [], B for <>, or C for ()." << endl;
    cin >> choice;
   
  };

double vectors::squarebracket()
{
 
  double squarevalue1 = 0;
  double squarevalue2 = 0;

  squarevalue1 = (py1 + i*px1)*sqrt((p02 - pz2)/(p01 - pz1));
  squarevalue2 = (py2 + i*px2)*sqrt((p01 - pz1)/(p02 - pz2));

  cout << squarevalue1 - squarevalue2;
 
}

double vectors::anglebracket()
{
 
  double anglevalue1 = 0;
  double anglevalue2 = 0;
 

  anglevalue1 = (py2 - i*px2)*sqrt((p01 - pz1)/(p02 - pz2));
  anglevalue2 = (py1 + i*px1)*sqrt((p02 - pz2)/(p01 - pz1));

  cout << anglevalue1 - anglevalue2;
}

double vectors::roundbracket()
{

  double roundvalue1 = 0;
  double roundvalue2 = 0;

  roundvalue1 = sqrt((p02 - pz2)/(p01 - pz1));
  roundvalue2 = sqrt((p01-pz1)/(p02 - pz2));

  cout << "(m_" << vect1 << ")" << roundvalue1 << " + " << "(m_" << vect2 << ")" << roundvalue2 << endl; 
}

int main()
{

  vectors vector;

  vector.input();

  if (choice == 'A')
    vector.squarebracket();
  else if (choice == 'B')
    vector.anglebracket();
  else if (choice == 'C')
    vector.roundbracket();
  else
    {
      cout << "That is not a valid option." << endl;
    }

 
}
 
A few things:

1) I 100% guarantee your input function isn't working like you expect it to. First of all, it never catches if invalid values are passed to it. Second of all, if a valid value is passed for the vect1 the if blocks for vect2 are never called. I reccomend looking into switch statements.

2) Comments as to what the variables are (or, even better, more verbose names) would be useful, since names like "px1" and "px2" tell coders nothing and make things tough to read.

3) A minor gripe, but in C++, vector (or at least vector in all lower case) is commonly expected to refer to the STL std::vector, so you might want to consider changing the class/variable names.

4) In response to your original question of handling imaginary numbers - there are several ways of handling this. One that comes to mind is this: Instead of dealing with doubles, deal with a struct instead; something like this:
Code:
struct complexNum {
    double val;
    bool imaginary;
}
 
Back
Top