template <typename U, typename V>
void multiply(const polynomial<U>& a, const polynomial<V>& b) {
if (!a || !b)
{
this->set_zero();
return;
}
std::vector<T> prod(a.size() + b.size() - 1, T(0));
for (unsigned i = 0; i < a.size(); ++i)
for (unsigned j = 0; j < b.size(); ++j)
prod[i+j] += a.m_data[i] * b.m_data[j];
m_data.swap(prod);
}
template <class U>
polynomial& operator *=(const polynomial<U>& value)
{
this->multiply(*this, value);
return *this;
}
Функцию для возведения полинома в степень, писал как раз для реализации этого метода.
polynomial<T> pow(polynomial<T> base, int exp)
{
if (exp < 0)
return policies::raise_domain_error(
"boost::math::tools::pow<%1%>",
"Negative powers are not supported for polynomials.",
base, policies::policy<>());
// if the policy is ignore_error or errno_on_error, raise_domain_error
// will return std::numeric_limits<polynomial<T>>::quiet_NaN(), which
// defaults to polynomial<T>(), which is the zero polynomial
polynomial<T> result(T(1));
if (exp & 1)
result = base;
/* "Exponentiation by squaring" */
while (exp >>= 1)
{
base *= base;
if (exp & 1)
result *= base;
}
return result;
}
#define number 10
#define cat(x, y) x # y
#define xcat(x, y) cat(x, y)
#define stamp xcat("stamp n.", number)
Помогите выбрать страну и университет: Украина, Россия или Европа ( Польша, Чехия)
Куда пойти учиться на программиста?Computer Science & Information Systems
Во что верить?
Implicit initialization
If an initializer is not provided:
objects with automatic storage duration are initialized to indeterminate values (which may be trap representations)
...
If an indeterminate value is used as an argument to any standard library call, the behavior is undefined. Otherwise, the result of any expression involving indeterminate values is an indeterminate value (e.g. int n;, n may not compare equal to itself and it may appear to change its value on subsequent reads)
Оконная парадигма
Метод скользящего окна возник из более общего принципа кадрирования.
Кадрирование заключается в получении состояния системы и ограничении области обзора только его частью, называемой «окном». Это создаёт разделение между алгоритмом кадрирования и алгоритмом, применяемым к тем элементам, которые видимы через окно, что упрощает оба алгоритма.
#ifndef A_H
#define A_H
#include<iostream>
class B;
class A
{
friend class B;
public:
explicit A(int);
int getVal() const;
template<typename X>
void setVal(const X &x)
{
value = x.value;
}
void print()
{
std::cout << value << std::endl;
}
private:
int value;
};
#endif // A_H
#ifndef B_H
#define B_H
#include<iostream>
class A;
class B
{
friend class A;
public:
explicit B(int);
int getVal() const;
template<typename X>
void setVal(const X &x)
{
value = x.value;
}
void print()
{
std::cout << value << std::endl;
}
private:
int value;
};
#endif // B_H
#include "a.h"
A::A(int x) : value(x)
{
}
#include "b.h"
B::B(int x) : value(x)
{
}
#include "a.h"
#include "b.h"
int main()
{
A a(11);
B b(21);
B b2(31);
std::cout << "Before:\n";
a.print();
b.print();
std::cout << "After:\n";
b.setVal(a);
b.print();
a.setVal(b2);
a.print();
}
Алгоритмы. Руководство по разработке (2-е издание)
Стивен С. Скиена
Глава 3. Структуры данных