Solving C++ Complex Array with Time-Dependent Schrodinger Equation

In summary, the programmer is trying to solve a problem with the time dependent schrodinger equation. They use complex numbers and the comma operator. They try to output data using the complex number but it does not work. They also try to take the complex conjugate of an array but it does not work either.
  • #1
castusalbuscor
16
0
Hi, I've been trying to write a program to solve a propagation of a wave packet using the time dependent schrodinger equation.
and I noticed I would need to use complex numbers.

I know that I need to use #include<complex>

I declare my array, and I started with a simple 1-d array.
complex<double>a[2]
When inputting data to the array I know that I would need something like this:
a[0] = (1,5);
a[1] = (2,8);


however when I output the data I only get:

a[0] = (5,0)
a[1] = (8,0)


Any idea how I can get around it?

And what about taking the complex conjugate of an array?
 
Technology news on Phys.org
  • #2
Unfortunately, it did exactly what you asked it to!

The (default) comma operator is defined to:
(1) Evaluate its first argument
(2) Discard the result
(3) Evaulate and return its second argument


So, the expression (1, 5) correctly evaluates to 5. For example, try this statement:

std::cout << (1, 5) << std::endl


Your problem is that you wanted to assign a complex number into the array, and so you must create the one you want to put into the array. An example of invoking the two-argument constructor to create a complex number is the expression

complex<double>(1, 5)

which evaluates to the complex number 1+5i.
 
  • #3
Hurkyl said:
Unfortunately, it did exactly what you asked it to!

The (default) comma operator is defined to:
(1) Evaluate its first argument
(2) Discard the result
(3) Evaulate and return its second argument


So, the expression (1, 5) correctly evaluates to 5. For example, try this statement:

std::cout << (1, 5) << std::endl


Your problem is that you wanted to assign a complex number into the array, and so you must create the one you want to put into the array. An example of invoking the two-argument constructor to create a complex number is the expression

complex<double>(1, 5)

which evaluates to the complex number 1+5i.

Yeah, I just tried it and it did not work.

using cout << complex<double>(1,5) does indeed output (1,5).

However, when I use
Code:
complex<double> a;
a = complex<double>(1,5);
cout << a;

I get (5,0). And when I use std::cout << (1, 5) << std::endl I still get (5,0)
 
  • #4
Are you sure you doing it right? It works fine here.

Code:
#include <iostream>
#include <complex>

int main()
{
    std::cout << std::complex<double>(1.0, 5.0) << std::endl;

    std::complex<double>a[2];
    a[0] = std::complex<double>(2.0, 4.0);
    a[1] = std::complex<double>(3.0, 3.0);

    std::cout << a[0] << '\n' << a[1] << std::endl;

    std::complex<double> b;
    b = std::complex<double>(4.0, 2.0);
    std::cout << b << std::endl;

    return 0;
}
gives
(1,5)
(2,4)
(3,3)
(4,2)
 

1. What is the Schrodinger equation?

The Schrodinger equation is a mathematical formula that describes how quantum systems, such as atoms and subatomic particles, evolve over time. It is a fundamental concept in quantum mechanics and is used to study the behavior of particles at the atomic and subatomic level.

2. How is the Schrodinger equation related to C++ programming?

C++ is a programming language commonly used in scientific computing, including quantum mechanics. The Schrodinger equation can be written as a differential equation, which can then be solved using numerical methods in C++. This allows scientists to simulate and study complex quantum systems.

3. What is a complex array in C++?

A complex array in C++ is a data structure that stores a collection of complex numbers. It is commonly used in scientific computing, including solving the Schrodinger equation. A complex number is a number that has both a real and imaginary component, and it is represented in C++ using the complex data type.

4. How does time-dependence affect the Schrodinger equation?

The Schrodinger equation is a time-dependent equation, which means that its solution depends on time. This is because quantum systems can evolve and change over time. By solving the time-dependent Schrodinger equation, scientists can study how quantum systems behave and change over time.

5. What are some applications of solving the Schrodinger equation in C++?

Solving the Schrodinger equation in C++ has many applications in physics, chemistry, and materials science. It is used to study and predict the behavior of atoms, molecules, and other quantum systems. It is also used in fields such as quantum computing, where understanding the behavior of quantum systems is crucial.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
945
  • Programming and Computer Science
Replies
1
Views
898
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
8K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top