本文整理汇总了C++中Complex类的典型用法代码示例。如果您正苦于以下问题:C++ Complex类的具体用法?C++ Complex怎么用?C++ Complex使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Complex类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: mul
void Complex::mul(Object *b)
{
if(b->getType() == "Complex")
{
Complex *c = (Complex *)b;
this->real = (this->real)*(c->getValue1())-(this->imag)*(c->getValue2());
this->imag = (this->real)*(c->getValue2())+(this->imag)*(c->getValue1());
}
else if(b->getType() == "Real")
{
Real *c = (Real *)b;
this->real *= c->getValue();
this->imag *= c->getValue();
}
else if(b->getType() == "Rational")
{
//Rational->Real
Real *c = (Real *)new Real(0.0);//这种垃圾如何回收?
Rational *d = (Rational *)b;
c->changeValue((double)(d->getValue1())/(d->getValue2()));
this->mul(c); //递归
delete c; //这里回收挖~
}
else if(b->getType() == "Integer")
{
//Integer->Rational
Rational *c = (Rational *)new Rational(0,1);
Integer *d = (Integer *)b;
c->changeValue1(d->getValue());
this->mul(c); //递归
delete c;
}
else
{
cerr << "Error calcution" << endl;
}
}
示例2: main
int main(){
TEST_CASE_A("Constructors")
Complex c;
TEST_ASSERT_EQ(c, 0, 0);
int a = 1234, b = 5678;
c = Complex(a, b);
TEST_ASSERT_EQ(c, a, b);
TEST_CASE_END
TEST_CASE_A("const math operators")
Complex c = Complex(12, 34);
c = c + Complex(12, 21);
TEST_ASSERT_EQ(c, 24, 55);
TEST_CASE_END
TEST_CASE_A("setting math operators")
Complex c = Complex(1234, 5678);
c += 9;
TEST_ASSERT_EQ(c, 1243, 5678);
c += Complex(-9, -5600);
TEST_ASSERT_EQ(c, 1234, 78);
c -= Complex(1200, 72);
TEST_ASSERT_EQ(c, 34, 6);
c *= Complex(-1, 1);
TEST_ASSERT_EQ(c, -40, 28);
c /= Complex(34, 6);
TEST_ASSERT_EQ(c, -1, 1);
c *= 6;
TEST_ASSERT_EQ(c, -6, 6);
TEST_CASE_END
TEST_CASE_A("eq opers")
Complex c = Complex(1234, 5678);
TEST_ASSERT(c == Complex(1234, 5678));
c *= 8765;
TEST_ASSERT(c == Complex(1234 *8765, 5678 *8765));
TEST_ASSERT(c != Complex(123, 234));
TEST_CASE_END
TEST_CASE_A("angle, radius")
const double PI = 3.14159265;
Complex c = Complex(1, 0);
output << c.radius() << " " << c.angle() << endl;
TEST_ASSERT(c.angle() == 0 && c.radius() == 1);
c = Complex(5, 5);
TEST_ASSERT(c.angle() - PI / 4 < 1E-5 && c.radius() - 5*sqrt(2) < 1E-5);
TEST_CASE_END
return 0;
}
示例3: i
Complex Complex::divide(Complex comp2)
{
/* Following formula
a + ib ( a * c + b * d ) i( c * b - a * d )
------ = ----------------- + -------------------
c + id c^2 + d^2 c^2 + d^2
*/
// Create Complex object called "temp"
Complex temp;
// Set temp data member "a" to ((a*c) + (b*d)) / (c^2 + d^2)
temp.set_a((this->get_a()*comp2.get_a() + this->get_b()*comp2.get_b()) / (comp2.get_a()*comp2.get_a() + comp2.get_b()*comp2.get_b()));
// Set temp data member "b" to ((c*b) - (a*d)) / (c^2 + d^2)
temp.set_b((comp2.get_a()*this->get_b() - this->get_a()*comp2.get_b()) / (comp2.get_a()*comp2.get_a() + comp2.get_b()*comp2.get_b()));
return temp;
}
示例4: secondWord
WordPair::WordPair(Word* first, Word* second, Params<Complex>* params)
:firstWord(first), secondWord(second), distanceIndex(0)
{
Complex centerDiff = first->matrix.a / first->matrix.c
- second->matrix.a / second->matrix.c;
int y0 = int(floor(centerDiff.imag() / params->lattice.imag()));
int x0 = int(floor(centerDiff.real() - y0*params->lattice.real()));
for (int x = -2-x0; x <= 3-x0; ++x) {
for (int y = -1-y0; y <= 2-y0; ++y) {
if (x == 0 && y == 0 && firstWord->name[0] == secondWord->name[0])
continue;
LatticePoint l;
l.params = params;
l.x = -x;
l.y = -y;
Complex t = centerDiff + double(x) + double(y)*params->lattice;
l.distance = norm(t);
if (abs(l.x) < 4 && abs(l.y < 4))
distances.push_back(l);
}
}
sort(distances.begin(), distances.end(), LowerDistance());
setCurrentIndex(0);
}
示例5: a
void ComplexTest::invertedNormalized() {
std::ostringstream o;
Error redirectError{&o};
Complex a(-0.6f, 0.8f);
Complex b(-0.6f, -0.8f);
(a*2).invertedNormalized();
CORRADE_COMPARE(o.str(), "Math::Complex::invertedNormalized(): complex number must be normalized\n");
Complex inverted = a.invertedNormalized();
CORRADE_COMPARE(a*inverted, Complex());
CORRADE_COMPARE(inverted*a, Complex());
CORRADE_COMPARE(inverted, b);
}
示例6: performTest
boost::tuple<std::string, int> performTest(Complex &complex)
{
CRef<ReducibleFreeChainComplexType> RFCComplexCR_orginal=
(ReducibleFreeChainComplexOverZFromSComplexAlgorithm<Complex, ReducibleFreeChainComplexType>(complex))();
CRef<HomologySignature<int> > homSignCR_orginal=HomAlgFunctors<FreeModuleType>::homSignViaAR_Random(RFCComplexCR_orginal);
CoreductionAlgorithm<AKQReduceStrategy<Complex> > algorithm(new AKQReduceStrategy<Complex>(complex));
algorithm();
BOOST_FOREACH(typename Complex::Iterators::AllCells::iterator::value_type v,
complex.iterators().allCells())
{
BOOST_CHECK_EQUAL(v.getColor(), (typename Complex::Color)2);
}
BOOST_CHECK(complex.iterators(1).allCells().begin() == complex.iterators(1).allCells().end());
SComplex<SComplexDefaultTraits>* AKQ = algorithm.getStrategy()->getOutputComplex();
CRef<ReducibleFreeChainComplexType> RFCComplexCR =
(ReducibleFreeChainComplexOverZFromSComplexAlgorithm<SComplex<SComplexDefaultTraits>, ReducibleFreeChainComplexType>(*AKQ))();
CRef<HomologySignature<int> > homSignCR=HomAlgFunctors<FreeModuleType>::homSignViaAR_Random(RFCComplexCR);
std::ostringstream signature;
signature << "AKQ: " << homSignCR() << " | org: " << homSignCR_orginal();
cerr << "AKQ: " << homSignCR() << " | org: " << homSignCR_orginal();
std::string sig = signature.str();
std::replace(sig.begin(), sig.end(), '\n', '#');
return boost::make_tuple(sig, AKQ->size());
}
示例7: main
int main( )
{
float a, b;
int state;
cout << "複素数: a + bi" << endl << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
Complex value1(a, b);
cout << "加算 = 0, 減算 = 1" << endl;
cin >> state;
cout << "複素数: a + bi" << endl << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
Complex value2(a, b);
Complex *value;
value = &value1;
if (state == 0) {
value->sum(value2);
}
else if (state == 1) {
value->sub(value2);
}
cout << "答え = ";
value1.printValue( );
return 0;
}
示例8: Complex
void WFMDemod::feed(SampleVector::const_iterator begin, SampleVector::const_iterator end, bool positiveOnly)
{
qint16 sample;
Real x, y, demod;
for(SampleVector::const_iterator it = begin; it < end; ++it) {
x = it->real() * m_last.real() + it->imag() * m_last.imag();
y = it->real() * m_last.imag() - it->imag() * m_last.real();
m_last = Complex(it->real(), it->imag());
demod = atan2(y, x);
Complex e(demod, 0);
Complex c;
if(m_interpolator.interpolate(&m_sampleDistanceRemain, e, &c)) {
sample = (qint16)(c.real() * 100 * m_volume * m_volume);
m_sampleBuffer.push_back(Sample(sample, sample));
m_audioBuffer[m_audioBufferFill].l = sample;
m_audioBuffer[m_audioBufferFill].r = sample;
++m_audioBufferFill;
if(m_audioBufferFill >= m_audioBuffer.size()) {
uint res = m_audioFifo->write((const quint8*)&m_audioBuffer[0], m_audioBufferFill, 1);
if(res != m_audioBufferFill)
qDebug("lost %u samples", m_audioBufferFill - res);
m_audioBufferFill = 0;
}
m_sampleDistanceRemain += (Real)m_sampleRate / 48000.0;
}
}
if(m_audioFifo->write((const quint8*)&m_audioBuffer[0], m_audioBufferFill, 0) != m_audioBufferFill)
qDebug("lost samples");
m_audioBufferFill = 0;
if(m_sampleSink != NULL)
m_sampleSink->feed(m_sampleBuffer.begin(), m_sampleBuffer.end(), true);
m_sampleBuffer.clear();
}
示例9: main
int main()
{
Complex x(1.1f, -2.7f);
Complex y(2.1f, 3.3f);
Complex z;
z = x+y;
cout << "x+y = ";
z.Display();
cout << endl;
z = x/y;
cout << "x/y = ";
z.Display();
cout << endl;
z = y/x;
cout << "y/x = ";
z.Display();
cout << endl;
// Matrix operations.
Matrix m1(3);
Matrix m2(3);
Matrix m3(3);
cout << "Enter matrices: " << endl;
m1.Input();
m2.Input();
m3 = m1 + m2;
cout << "Result of addition is: ";
m3.Display();
return 0;
}
示例10: main
int main()
{
Complex c;
Complex d = Complex(3,4);
Complex a(1,5);
Complex b(a);
a.Print();
b.Print();
c.Print();
d.Print();
std::cout<<"operator overloading tests"<<std::endl;
Complex e(a+b);
c = a+b;
a.Print();
c.Print();
e.Print();
return 0;
}
示例11: a
void ComplexTest::invertedNormalized() {
std::ostringstream o;
Error::setOutput(&o);
Complex a(-0.6f, 0.8f);
Complex b(-0.6f, -0.8f);
Complex notInverted = (a*2).invertedNormalized();
CORRADE_VERIFY(notInverted != notInverted);
CORRADE_COMPARE(o.str(), "Math::Complex::invertedNormalized(): complex number must be normalized\n");
Complex inverted = a.invertedNormalized();
CORRADE_COMPARE(a*inverted, Complex());
CORRADE_COMPARE(inverted*a, Complex());
CORRADE_COMPARE(inverted, b);
}
示例12: lock
int gr::gs::Implementations::Autocovariance_impl<gr::gs::Complex>
::work(
int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items)
{
std::lock_guard<std::mutex> lock(m_mutex);
const Complex* input =
reinterpret_cast<const Complex*>(input_items[0])
+this->history()-1+m_offset;
const Complex* const inputEnd =
input + noutput_items*this->decimation();
std::array<float*,4> output
{
reinterpret_cast<float*>(output_items[0]),
reinterpret_cast<float*>(output_items[1]),
reinterpret_cast<float*>(output_items[2]),
reinterpret_cast<float*>(output_items[3])
};
while(input<inputEnd)
{
Complex currentValue = *input - m_mean;
float* const outputEnd = output[0] + m_length;
const Complex* pastValue_ptr = input - (this->history()-1);
while(output[0]<outputEnd)
{
const Complex pastValue = *pastValue_ptr++ - m_mean;
*output[0]++ = currentValue.real() * pastValue.real();
*output[1]++ = currentValue.real() * pastValue.imag();
*output[2]++ = currentValue.imag() * pastValue.real();
*output[3]++ = currentValue.imag() * pastValue.imag();
}
input += this->decimation();
}
return (output[0]-reinterpret_cast<float*>(output_items[0]))
/m_length;
}
示例13: func
void MainWindow::setGraphBoundsTo(fx_t<Complex> func)
{
if (func == nullptr) {
return;
}
double minY = func(solverComplex->getA()).real();
double maxY = minY;
double step = solverComplex->getDx();
for (double x = solverComplex->getA(); x <= solverComplex->getB(); x += step) {
Complex cur = func(x);
if (cur.real() < minY) minY = cur.real();
if (cur.imag() < minY) minY = cur.imag();
if (cur.real() > maxY) maxY = cur.real();
if (cur.imag() > maxY) maxY = cur.imag();
}
ui->plotWidget->yAxis->setRange(minY - 0.2, maxY + 0.2);
ui->plotWidget->xAxis->setRange(ui->lowerBoundInput->value(), ui->upperBoundInput->value());
//ui->plotWidget->yAxis->setRange(-3.0, 3.0);
}
示例14: multiply
Complex Complex::multiply(Complex comp2)
{
// Following formula (a + ib ) * ( c + id ) = ( a * c - b * d ) + i( a * d + b * c )
// Create Complex object called "temp"
Complex temp;
// Set temp data member "a" to (this->get_a() * comp2.get_a()) - (this->get_b() * comp2.get_b())
temp.set_a((this->get_a() * comp2.get_a()) - (this->get_b() * comp2.get_b()));
// Set temp data member "b" to ((this->get_a() * comp2.get_b()) + (this->get_b() * comp2.get_a()))
temp.set_b((this->get_a() * comp2.get_b()) + (this->get_b() * comp2.get_a()));
// Return resulting Complex object "temp"
return temp;
}
示例15: main
int main()
{
// Test constructor -NOTE: your Complex class should have only
// one constructor. Your constructor should have default arguments
// for the real and imaginary parts of the complex object.
Complex A; // create a Complex object using default arguments
cout << "The default object is: ";
A.display();
Complex B(-2.45, 1.0); // create a Complex object supplying values to the ctor
cout << "\n\nThe non-default object is: ";
B.display();
Complex C(25.777,-35.668); // create another Complex object supplying values to the ctor
cout << "\n\nThe second non-default object is: ";
C.display();
// Test plusEq()
cout << "\n\n- Test plusEq()";
A = Complex(-25.44,-3.543); //Assign new values to Complex objects
B = Complex(30.3,-34.876);
// NOTE: Equivalent to: C = A += B;
C = A.plusEq(B);
cout << "\nA = ";
A.display();
cout << "\nB = ";
B.display();
cout << "\nC = ";
C.display();
// Test minusEq()
cout << "\n\n- Test minusEq()";
A = Complex(4.65,3.789); //Assign new values to Complex objects
B = Complex(6.78,9.222);
// NOTE: Equivalent to: C = A -= B;
C = A.minusEq(B);
cout << "\nA = ";
A.display();
cout << "\nB = ";
B.display();
cout << "\nC = ";
C.display();
cout << '\n' << endl;
return 0;
} // end main