Skip to content

Instantly share code, notes, and snippets.

@t-uda
Created June 19, 2012 15:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t-uda/2954837 to your computer and use it in GitHub Desktop.
Save t-uda/2954837 to your computer and use it in GitHub Desktop.
boost::numeric::interval<int>(2): C++ Boost 区間演算ライブラリ 数学関数 ref: http://qiita.com/items/abcf31a7b1787604a4f4
a = [-1,3]
b = [2,4]
min(a, b) = [-1,3]
max(a, b) = [2,4]
abs(a) = [0,3]
--- interval power functions ---
a = [-1,3]
square(a) = [0,9]
pow(a, 3) = [-1,27]
b = [2,4]
nth_root(b, 2) = [1.41421,2]
--- checking inclusion property ---
c = [-2,2.66667]
k d = c^k e = d^(1/k) inclusion
1 [-2,2.66667] [-2,2.66667] c is a subset of e.
2 [0,7.11111] [0,2.66667] abs(c) is a subset of e.
3 [-8,18.963] [-2,2.66667] c is a subset of e.
4 [0,50.5679] [0,2.66667] abs(c) is a subset of e.
5 [-32,134.848] [-2,2.66667] c is a subset of e.
a = [1,2]
b = [-3,4]
a / b = [-inf,inf]
negative part of a / b = [-inf,-0.333333]
positive part of a / b = [0.25,inf]
#include <iostream>
#include <boost/numeric/interval.hpp>
#include <boost/numeric/interval/io.hpp>
int main () {
typedef double R;
typedef boost::numeric::interval<R> IR;
using std::cout;
using std::endl;
const IR a(-1, 3);
const IR b(2, 4);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "min(a, b) = " << min(a, b) << endl;
cout << "max(a, b) = " << max(a, b) << endl;
cout << "abs(a) = " << abs(a) << endl;
return 0;
}
#include <iostream>
#include <boost/numeric/interval.hpp>
#include <boost/numeric/interval/io.hpp>
int main () {
typedef double R;
typedef boost::numeric::interval<R> IR;
using std::cout;
using std::endl;
cout << "--- interval power functions ---" << endl;
const IR a(-1, 3);
cout << "a = " << a << endl;
cout << "square(a) = " << square(a) << endl;
cout << "pow(a, 3) = " << pow(a, 3) << endl;
const IR b(2, 4);
cout << "b = " << b << endl;
cout << "nth_root(b, 2) = " << nth_root(b, 2) << endl;
cout << "--- checking inclusion property ---" << endl;
const IR c = IR(-6, 8) / 3.0;
cout << "c = " << c << endl;
cout << " k \t d = c^k \t e = d^(1/k) \t inclusion" << endl;
for ( int k = 1; k <= 5; k++ ) {
const IR d = pow(c, k);
const IR e = nth_root(d, k);
cout << k << '\t' << d << '\t' << e << '\t';
if (k % 2 == 1 && subset(c, e)) {
cout << " c is a subset of e." << endl;
} else if (k % 2 == 0 && subset(abs(c), e)) {
cout << "abs(c) is a subset of e." << endl;
} else {
cout << endl;
}
}
return 0;
}
#include <iostream>
#include <boost/numeric/interval.hpp>
#include <boost/numeric/interval/io.hpp>
int main () {
typedef double R;
typedef boost::numeric::interval<R> IR;
using std::cout;
using std::endl;
const IR a(1, 2);
const IR b(-3, 4);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "a / b = " << a / b << endl;
bool parted = false;
const IR c = division_part1(a, b, parted);
const IR d = division_part2(a, b, parted);
if (parted) {
cout << "negative part of a / b = " << c << endl;
cout << "positive part of a / b = " << d << endl;
}
return 0;
}
@NanoJeoMichael
Copy link

That's cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment