Skip to content

Instantly share code, notes, and snippets.

View marionette-of-u's full-sized avatar

arithmeticae marionette-of-u

View GitHub Profile
@marionette-of-u
marionette-of-u / gist:52179d0d2fe0a26d497f
Created August 11, 2015 11:36
代数方程式の数値解を全部出力しようとして複素数に対応してなかったせいで途中でハングするプログラムです
std::vector<numeric> nth_root(const ex &f_, const symbol &x, const numeric epsilon = std::numeric_limits<double>::epsilon()){
ex f = f_;
std::vector<numeric> result;
int deg = f.degree(x);
result.reserve(deg);
ex g = f.diff(x);
numeric xn = deg / 2.0, xm;
for(; ; ){
xm = xn - ex_to<numeric>(f.subs(x == xn)) / ex_to<numeric>(g.subs(x == xn));
if(abs(xn - xm) < epsilon){
@marionette-of-u
marionette-of-u / gist:1f92f5d3a9e89e938718
Created August 8, 2015 13:32
C++11によるパーサジェネレータのブートストラップの記述例
https://gist.github.com/marionette-of-u/cd23016454f22bfb3383/019f24bfbdf60ba9025cd4ea664b051ebe678575
decl_g関数はBNF規則名を取り規則を保持するscanner::rule_rhsのインスタンスの参照を返す
中身はstd::map<std::string, scanner::rule_rhs>とほぼ同じ
nt関数は非終端記号を内部処理可能な記号(=term(=int))に変換する
seqクラスはコンストラクタに記号(=term(=int))の初期化リストとセマンティックアクションを取る
初期化リストはそのままBNF規則の右辺のひとつとして扱える
ASTを構築してあとで解析するのでほとんどのセマンティックアクションはeat(食べる)
@marionette-of-u
marionette-of-u / gist:b214118027f821691570
Created August 8, 2015 13:31
C++03によるパーサジェネレータのブートストラップの記述例
https://github.com/marionette-of-u/kp19pp/blob/stable/scanner.cpp
フォーマット解説 :
DECL_SEQSマクロでBNF表現ルールを記述
DECL_SEQS_EPSはε還元を含むルールを追加する以外DECL_SEQSと同機能
L = Ra1 Ra2 Ra3 [ActA] | Rb1 Rb2 Rb3 [ActB];
を記述するには始めに左辺Lをマクロの引数に取る
Rはa, bと複数個あるのでここでBOOST_PP_SEQ形式(Ra)(ActA)(Rb)(ActB)の形を取る
@marionette-of-u
marionette-of-u / symbolic_integration.cpp
Last active August 29, 2015 14:26
Symbolic Integration vol.1
#include <iostream>
#include <vector>
#include <functional>
#include <utility>
#include <memory>
#include <stdexcept>
#include <iterator>
#include <algorithm>
#include <limits>
@marionette-of-u
marionette-of-u / integer.hpp
Last active August 29, 2015 14:26
多倍精度整数を使った有理数
#ifndef MULTI_PRECISION_INCLUDE_INTEGER_HPP
#define MULTI_PRECISION_INCLUDE_INTEGER_HPP
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <iterator>
#include <utility>
#include <random>
疑似言語Tiel・構想
// カンマ演算子で得た値を再帰的に保存する
class comma<class TypeA, class TypeB>{
TypeA lhs;
TypeB rhs;
};
// ブラケット演算子で得た値を保存する
using bracket<class TypeA, class TypeB> = comma<TypeA, TypeB>;
@marionette-of-u
marionette-of-u / trs_lie-ring.ml
Created July 10, 2015 07:10
OCamlによるTerm Rewriting Systemの実装とリー環への完備化の適用
exception INVALID_ARGUMENT
let rec nth =
function (x :: _, 0) -> x
| (x :: xs, n) -> nth(xs, n - 1)
| ([], _) -> raise INVALID_ARGUMENT
let null s =
match s with
| [] -> true
@marionette-of-u
marionette-of-u / trs.cpp
Last active May 22, 2021 00:53
Knuth-Bendix Completion Algorithm. (C++)
#include <algorithm>
#include <vector>
#include <tuple>
#include <functional>
#include <utility>
#include <memory>
#include <boost/variant.hpp>
template<class ValueType, class Less = std::less<ValueType>>
class term_rewriting_system{
<token> token{
<right>{ unary_minus; }
<left>{
mul = "\*", div = "\/";
add = "\+", sub = "-";
}
left_paren = "\(", right_paren = "\)";
id<int> = "[a-zA-Z_][a-zA-Z0-9_]*";
}
<grammar> parser{
@marionette-of-u
marionette-of-u / tt_legacy_bmp.hpp
Created June 15, 2015 09:48
bmpファイルを出力、編集する。MSVCの古いバージョンで動作確認済み。gcc 4.9.1ではコンパイル通ったものの動作未確認。boost非依存。
/*
----class/struct
dot
1ドットを表す構造体
b1u_t b; //blue
b1u_t g; //green
b1u_t r; //red
b1u_t a; //alpha
logic_error