differentials

calculator for differentials of x^n sequences
git clone git://git.yotsev.xyz/differentials.git
Log | Files | Refs | README | LICENSE

commit c12547b518de514696693d496d557a57fdbf00b3
Author: Petar Yotsev <petar@yotsev.xyz>
Date:   Wed,  5 May 2021 14:37:02 +0100

Initial commit

Diffstat:
AMakefile | 16++++++++++++++++
AREADME.md | 31+++++++++++++++++++++++++++++++
Amain.cpp | 183+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 230 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,16 @@ +CC = g++ +CC_FLAGS = -g -O2 + +EXEC = prog +SOURCES = $(wildcard *.cpp) +OBJECTS = $(SOURCES:.cpp=.o) + +$(EXEC): $(OBJECTS) + $(CC) $(OBJECTS) -o $(EXEC) $(CC_FLAGS) + rm -f $(OBJECTS) + +%.o: %.cpp + $(CC) -c $(CC_FLAGS) $< -o $@ + +clean: + rm -f $(EXEC) $(OBJECTS) diff --git a/README.md b/README.md @@ -0,0 +1,31 @@ +## Dependencies + +* Boost libraries and headers, namely multiprecision + +This dependency is not really required anymore and it will soon be +removed but for now you need it to compile the program. + +## Compilation + + $ make + +## Usage + + $ ./prog + $ ./prog post + $ ./prog prior + +After the program is done printing, you are dropped into interactive +mode where you can enter the name of a sequence to print it again or do +simple addition/subtraction on it and print the result. The syntax of +the three are respectively: + + d2n4 + d2n4 3 + d2n4 -5 + +To exit interactive mode press Ctrl-C + +If you want to change the maximum power or the number of numbers in a +sequence, go to `main.cpp`, change the value of `POWER` and +`SEQUENCE_SIZE`` respectively and recompile. diff --git a/main.cpp b/main.cpp @@ -0,0 +1,183 @@ +#include <boost/multiprecision/cpp_int.hpp> +#include <iostream> +#include <map> +#include <math.h> +#include <vector> + +#define SEQUENCE_SIZE 7 +#define POWER 7 // also the maximum space a number in the sequence takes + +using namespace std; +using namespace boost::multiprecision; + +/* the functions doing actual calculations */ +void populate(const string& deffinition); +/* --------------------------------------- */ + +void pseq(const string& str); +void psubseq(const string& str); +void interactive(); + +map<string, vector<int>> sequences; + +int main(int argc, char** argv) +{ + if (argv[1] && argv[1] == (string) "post") + populate("post"); + else + populate("prior"); + + interactive(); + + return 0; +} + +void pseq(const string& str) +{ + vector<int> sequence = sequences[str]; + for (int i = 0; i < sequence.size(); ++i) { + cout << sequence[i] << " "; + } + cout << endl; +} + +void psubseq(const string& str) +{ + // splitting the input str by spaces + istringstream iss(str); + vector<string> results((istream_iterator<string>(iss)), + istream_iterator<string>()); + + /* if (results.size() == 1) { */ + /* vector<int> sequence = sequences[str]; */ + /* for (int i = 0; i < sequence.size(); ++i) { */ + /* cout << sequence[i] << " "; */ + /* } */ + /* cout << endl; */ + /* } else { */ + vector<int> sequence = sequences[results[0]]; + for (int i = 0; i < sequence.size(); ++i) { + cout << sequence[i] + stoi(results[1]) << " "; + } + cout << endl; + /* } */ +} + +void interactive() +{ + string str; + while (true) { + getline(cin, str, '\n'); + if (str.find(" ") != string::npos) + psubseq(str); + else + pseq(str); + } +} + +void populate(const string& deffinition) +{ + for (int power = 1; power <= POWER; ++power) { + // getting sequence name e.g. d1x1 + string seq_name = "x"; + seq_name.append(to_string(power)); + string diff_name = "d"; + diff_name.append(to_string(1)); + string res_name = diff_name.append(seq_name); + ; + cout << res_name << " = "; + + // calculates the sequence + vector<int> n; + int num; + for (int place = -SEQUENCE_SIZE; place <= SEQUENCE_SIZE; ++place) { + // raises the num in the sequence to the correct power + num = pow(place, power); + if (place == 0) + num = 0; + /* num += 1; */ + n.push_back(num); + for (int l = 0; l <= POWER - to_string(num).length(); ++l) + cout << " "; + cout << num; + } + cout << endl; + // stores the sequence in the map + sequences.insert(pair<string, vector<int>>(res_name, n)); + + // calculates the differentials of the sequences + for (int diff = 2; true; ++diff) { + // gets the sequence name e.g. d2n3 (aka ddn^3) + string diff_name = "d"; + diff_name.append(to_string(diff)); + string res_name = diff_name.append(seq_name); + + // finds the sequence it's supposed to differentiate + vector<int> s; + if (diff == 2) { + s = n; + } else { + string prev_diff_name = "d"; + prev_diff_name.append(to_string(diff - 1)); + prev_diff_name.append(seq_name); + s = sequences[prev_diff_name]; + } + + // differentiates + cout << diff_name << " = "; + + if (deffinition == "prior") + for (int l = 0; l < (diff)-1; ++l) { + for (int i = 0; i < POWER; ++i) + cout << " "; + cout << " "; + } + + vector<int> m; + int n; + for (int i = 0; i < s.size() - 1; ++i) { + n = s[i + 1] - s[i]; + m.push_back(n); + for (int l = 0; l <= POWER - to_string(n).length(); ++l) + cout << " "; + cout << n; + } + + // stores the differential in the map + sequences.insert(pair<string, vector<int>>(res_name, m)); + + cout << endl; + + int l = m.front(); + for (int i = 0; i < m.size(); ++i) { + if (m[i] != m.front()) + goto cont; + } + break; + cont: + continue; + } + if (power != POWER) + cout << endl; + } +} + +void integrate() +{ + // calculates the sequence + vector<int> n; + int num; + for (int place = -SEQUENCE_SIZE; place <= SEQUENCE_SIZE; ++place) { + // raises the num in the sequence to the correct power + /* num = pow(place, power); */ + num = place; + if (place == 0) + num = 0; + /* num += 1; */ + n.push_back(num); + for (int l = 0; l <= POWER - to_string(num).length(); ++l) + cout << " "; + cout << num; + } + cout << endl; +}