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

  • Context: C/C++ 
  • Thread starter Thread starter castusalbuscor
  • Start date Start date
  • Tags Tags
    Array C++ Complex
Click For Summary

Discussion Overview

The discussion revolves around programming in C++ to solve the time-dependent Schrödinger equation, specifically focusing on the use of complex numbers and arrays. Participants are addressing issues related to the correct initialization and output of complex numbers in an array.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their attempt to create a 1-dimensional array of complex numbers and encounters issues with outputting the expected values.
  • Another participant explains that the use of the comma operator in C++ leads to unexpected results when initializing complex numbers, suggesting the correct way to create a complex number using the constructor.
  • A later reply indicates that despite following the suggested method, the participant still receives incorrect output when assigning a complex number to a variable.
  • Another participant provides a working example of code that correctly initializes and outputs complex numbers, implying that the original poster may be making an error in their implementation.

Areas of Agreement / Disagreement

There is no consensus on the source of the original poster's issue, as some participants believe the code should work as intended, while the original poster continues to experience problems.

Contextual Notes

Participants have not fully resolved the discrepancies in output, and there may be missing details regarding the original poster's code implementation or environment that could affect the results.

castusalbuscor
Messages
15
Reaction score
0
Hi, I've been trying to write a program to solve a propagation of a wave packet using the time dependent Schrödinger 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
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::count << (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.
 
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)
 
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)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 5 ·
Replies
5
Views
9K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K