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; } }
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; }
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; }
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); }
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); }
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()); }
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; }
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(); }
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; }
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; }
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); }
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; }
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); }
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; }
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
void add_cofaces (const Graph& graph, Simplex& tau, const Neighbors& neighbors, Complex& complex, const std::size_t dimension) { typedef typename Neighbors::const_iterator Neighbor_iterator; complex.insert_open_cell(tau); if(tau.dimension() >= dimension) { return; } Neighbors lower_neighbors; Neighbors final_neighbors; for(Neighbor_iterator i = neighbors.begin(); i != neighbors.end(); ++i){ lower_neighbors.clear(); Simplex sigma( tau); sigma.insert( *i); get_lower_neighbors(graph, *i, lower_neighbors); final_neighbors.clear(); set_intersection(lower_neighbors.begin(),lower_neighbors.end(), neighbors.begin(),neighbors.end(), back_inserter(final_neighbors)); add_cofaces(graph, sigma, final_neighbors, complex, dimension); } }
void test_PH5Curve() { cout << "TEST : test_PH5Curve()" << endl; PH5Curve<PH5TYPE> ph(ph_arc()); long msStart = millis(); PH5TYPE epsilon = 0.00001; int16_t ITER = 10000; for (int16_t i = 0; i < ITER; i++) { Complex<PH5TYPE> c; c = ph.r(0); c.assertEqualT(Complex<PH5TYPE>(-1, 1), epsilon); c = ph.r(0.25); c.assertEqualT(Complex<PH5TYPE>(-0.598911, 1.75), epsilon); c = ph.r(0.5); c.assertEqualT(Complex<PH5TYPE>(0, 2), epsilon); c = ph.r(0.75); c.assertEqualT(Complex<PH5TYPE>(0.598911, 1.75), epsilon); c = ph.r(1.0); c.assertEqualT(Complex<PH5TYPE>(1, 1), epsilon); } long msElapsed = millis() - msStart; cout << "TEST : average execution time for r(): " << msElapsed / (ITER / 5000) << "us" << endl; ASSERTEQUALT(0.00000, ph.s(0.0), epsilon); ASSERTEQUALT(1.52753, ph.s(0.5), epsilon); ASSERTEQUALT(3.05505, ph.s(1.0), epsilon); ASSERTEQUALT(4.11010, ph.sigma(0.0), epsilon); ASSERTEQUALT(2.78074, ph.sigma(0.3), epsilon); ASSERTEQUALT(2.52753, ph.sigma(0.5), epsilon); ASSERTEQUALT(2.78074, ph.sigma(0.7), epsilon); ASSERTEQUALT(4.11010, ph.sigma(1.0), epsilon); epsilon = 0.001; ph.rprime(0.00).assertEqualT(Complex<PH5TYPE>(0.945, 4.000), epsilon); ph.rprime(0.25).assertEqualT(Complex<PH5TYPE>(2.132, 2.000), epsilon); ph.rprime(0.50).assertEqualT(Complex<PH5TYPE>(2.528, 0.000), epsilon); ph.rprime(0.75).assertEqualT(Complex<PH5TYPE>(2.132, -2.000), epsilon); ph.rprime(1.00).assertEqualT(Complex<PH5TYPE>(0.945, -4.000), epsilon); cout << "TEST : test_PH5Curve() OK " << endl; }
void create_phat_filtration( const std::string& input_filename, bool dualize, int64_t upper_dim, const std::string& output_filename ) { Complex complex; complex.load_binary( input_filename, upper_dim ); dipha::data_structures::distributed_vector< int64_t > filtration_to_cell_map; dipha::algorithms::get_filtration_to_cell_map( complex, dualize, filtration_to_cell_map ); dipha::data_structures::distributed_vector< int64_t > cell_to_filtration_map; dipha::algorithms::get_cell_to_filtration_map( complex.get_num_cells( ), filtration_to_cell_map, cell_to_filtration_map ); const int64_t nr_columns = complex.get_num_cells( ); std::vector< std::vector< int64_t > > matrix( nr_columns ); std::vector< int64_t > dims( nr_columns, -1 ); for( int64_t cur_dim = 0; cur_dim <= complex.get_max_dim(); cur_dim++ ) { dipha::data_structures::flat_column_stack unreduced_columns; dipha::algorithms::generate_unreduced_columns( complex, filtration_to_cell_map, cell_to_filtration_map, cur_dim, dualize, unreduced_columns ); dipha::data_structures::heap_column col; while( !unreduced_columns.empty( ) ) { int64_t index; unreduced_columns.pop( index, col ); std::sort( col.begin( ), col.end( ) ); matrix[ index ] = col; dims[ index ] = dualize ? complex.get_max_dim( ) - cur_dim : cur_dim; } } std::ofstream output_stream( output_filename.c_str( ), std::ios_base::binary | std::ios_base::out ); output_stream.write( (char*)&nr_columns, sizeof( int64_t ) ); for( int64_t cur_col = 0; cur_col < nr_columns; cur_col++ ) { int64_t cur_dim = dims[ cur_col ]; output_stream.write( (char*)&cur_dim, sizeof( int64_t ) ); int64_t cur_nr_rows = matrix[ cur_col ].size( ); output_stream.write( (char*)&cur_nr_rows, sizeof( int64_t ) ); for( int64_t cur_row_idx = 0; cur_row_idx < cur_nr_rows; cur_row_idx++ ) { int64_t cur_row = matrix[ cur_col ][ cur_row_idx ]; output_stream.write( (char*)&cur_row, sizeof( int64_t ) ); } } output_stream.close( ); }
int main() { Real a1, a2; a1=1.0; a2=2.0; Complex a (a1,a2); cout << "( " << a.real() << "," << a.imag() << " )" << endl; a.setRealPart(3); cout << "( " << a.real() << "," << a.imag() << " )" << endl; return 0; }
int hashCode(const Complex& c) { return hashCode(c.real(), c.imag()); }
Complex Complex::operator / (const Complex & right)const{ return (*this)*right.MakeConjugate() / (right.real_part*right.real_part + right.complex_part*right.complex_part); }
Complex operator / (const double & left, const Complex & right){ return right.MakeConjugate() * left / pow(right.Module(), 2); }
void Phx::Check( Shape * A , Shape * B ) { Collision * c ; // Tworzymy strukturê danych o kolizji if( A->type == circle && B->type == circle ) // Sprawdzamy typy obiektów { Circle * circleA = static_cast<Circle*>(A); Circle * circleB = static_cast<Circle*>(B); if( A->global == false ) circleA->calc_glob(); if( B->global == false ) circleB->calc_glob(); c = CircleVsCircle(circleA , circleB); if( c != NULL ) Collisions.push_back(c); // Jeœli tak to zapisujemy t¹ kolizjê return; // Koñczymy funkcjê } if( A->type == polygon && B->type == polygon ) { Poly * polygonA = static_cast<Poly*>(A); // Rzutuje wskaŸniki Poly * polygonB = static_cast<Poly*>(B); if( A->global == false ) polygonA->calc_glob(); if( B->global == false ) polygonB->calc_glob(); c = PolygonVsPolygon(polygonA,polygonB); if( c != NULL ) Collisions.push_back(c); return; } if( A->type == polygon && B->type == circle ) { Poly * polygonA = static_cast<Poly*>(A); // Rzutuje wskaŸniki Circle * circleB = static_cast<Circle*>(B); if( A->global == false ) polygonA->calc_glob(); if( B->global == false ) circleB->calc_glob(); c = PolygonVsCircle(polygonA,circleB); if( c != NULL ) Collisions.push_back(c); return; } if( A->type == circle && B->type == polygon ) { Poly * polygonB = static_cast<Poly*>(B); // Rzutuje wskaŸniki Circle * circleA = static_cast<Circle*>(A); if( A->global == false ) circleA->calc_glob(); if( B->global == false ) polygonB->calc_glob(); c = PolygonVsCircle(polygonB,circleA); if( c != NULL ) Collisions.push_back(c); return; } if( A->type == complex ) { Complex * com = static_cast<Complex*>(A); if(com-> global == false ) com->calc_glob(); for(int i=0;i<com->size;i++) { Check( com->childs[i].sh , B ); } } if( B->type == complex ) { Complex * com = static_cast<Complex*>(B); if(com-> global == false ) com->calc_glob(); for(int i=0;i<com->size;i++) Check( com->childs[i].sh , A ); } }
Complex Complex::Sqrt( Complex &c ) { Float32 a=sqrt(c.GetPolarMagnitude()); Float32 b=0.5*c.GetPolarAngle(); return Polar( a,b ); }
bool operator ==(const Complex& c1, const Complex& c2) { return floatingPointEqual(c1.real(), c2.real()) && floatingPointEqual(c1.imag(), c2.imag()); }
bool operator <(const Complex& c1, const Complex& c2) { return c1.real() < c2.real() || (floatingPointEqual(c1.real(), c2.real()) && c1.imag() < c2.imag()); }
int main(){ Complex complex; complex.both(32, 23); return 0; }
int operator <=(const Complex& x1,const Complex& x2){ return x1.norm()<=x2.norm(); }
int operator >(const Complex& x1,const Complex& x2){ return x1.norm()>x2.norm(); }
// Comparison function for sort() bool comComplex(Complex a, Complex b) { return a.getAbs() > b.getAbs(); }