Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
// 4/12/2017: n-ary complex storage example by Chris M. Thomasson #include <complex> #include <iostream> #include <vector> #include <limits> #include <algorithm> // reverse #include <cstdint> // want to work with 64-bit numbers #include <cassert> // want to sanity check run time... #include <cstring> // faster than iostream's? // Well, I need some consistent typenames... ;^) typedef std::int64_t ct_int; typedef std::uint64_t ct_uint; typedef double ct_float; typedef std::numeric_limits<ct_float> ct_float_nlim; typedef std::complex<ct_float> ct_complex; typedef std::complex<ct_uint> ct_complex_uint; typedef std::vector<ct_complex> ct_complex_vec; #define CT_PI 3.14159265358979323846 // Round up and convert the real and imaginary // parts of z to unsigned integers of type ct_uint // return a complex number with unsigned integer parts ct_complex_uint ct_round_uint( ct_complex const& z ) { ct_uint re = (ct_uint)std::floor(std::abs(z.real()) + .5); ct_uint im = (ct_uint)std::floor(std::abs(z.imag()) + .5); return ct_complex_uint(re, im); } // the integer p shall not be zero // create abs(p) roots of z wrt z^(1/p); // store them in out, and return the average error. ct_float ct_roots( ct_complex const& z, ct_int p, ct_complex_vec& out ) { assert(p != 0); // Gain the basics ct_float radius = std::pow(std::abs(z), 1.0 / p); ct_float angle_base = std::arg(z) / p; ct_float angle_step = (CT_PI * 2.0) / p; // Setup the iteration ct_uint n = std::abs(p); ct_float avg_err = 0.0; // Calculate the n roots... for (ct_uint i = 0; i < n; ++i) { // our angle ct_float angle = angle_step * i; // our point ct_complex c = { std::cos(angle_base + angle) * radius, std::sin(angle_base + angle) * radius }; // output data out.push_back(c); // Raise our root the the power... ct_complex raised = std::pow(c, p); // Sum up the Go% damn floating point errors! avg_err = avg_err + std::abs(raised - z); } // gain the average error sum... ;^o return avg_err / n; } // Try's to find the target root z out of roots using // eps, return the index of the root, or -1 for failure. int ct_try_find( ct_complex const& z, ct_complex_vec const& roots, ct_float eps ) { std::size_t n = roots.size(); for (std::size_t i = 0; i < n; ++i) { ct_complex const& root = roots[i]; ct_float adif = std::abs(root - z); if (adif < eps) { return i; } } return -1; } // The Token Table // Will deal with scrambled token vectors in further posts. // This is global for now, easy to convert to per store/load // pairs static std::string const g_tokens_str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // Gains the power from the largest token position // from tokens in g_tokens_str ct_int ct_gain_power( std::string const& tokens ) { ct_uint n = tokens.length(); std::size_t pmax = 0; for (ct_uint i = 0; i < n; ++i) { std::size_t fridx = g_tokens_str.find_first_of(tokens[i]); assert(fridx != std::string::npos); pmax = std::max(pmax, fridx); } return (ct_int)(pmax + 1); } // Store tokens using a power of p starting in z-origin // Return the complex number holding said tokens. ct_complex ct_store( ct_complex const& z_origin, ct_int p, std::string const& tokens ) { ct_uint n = tokens.length(); ct_complex z = z_origin; ct_float store_avg_err = 0.0; std::cout << "Storing Data..." << "\n"; std::cout << "stored:z_origin:" << z_origin << "\n"; for (ct_uint i = 0; i < n; ++i) { // Gain all of the roots, and running store error ct_complex_vec roots; ct_float avg_err = ct_roots(z, p, roots); store_avg_err = store_avg_err + avg_err; // reference our root std::size_t fridx = g_tokens_str.find_first_of(tokens[i]); assert(fridx != std::string::npos); z = roots[fridx]; std::cout << "stored[" << i << "]:" << z << "\n"; } store_avg_err = store_avg_err / n; std::cout << "store_avg_err:" << store_avg_err << "\n"; return z; } // Load our tokens from z_store, power of p, // stopping at z_target using eps, storing tokens // in out_tokens, and the resulting z in out_z ct_float ct_load( ct_complex const& z_store, ct_complex const& z_target, ct_int p, ct_float eps, // epsilon std::string& out_tokens, ct_complex& out_z ) { ct_complex z = z_store; ct_uint n = 128; // max iter ct_float load_err_sum = 0.0; std::cout << "Loading Data..." << "\n"; for (ct_uint i = 0; i < n; ++i) { // raise... ct_complex z_next = std::pow(z, p); // Gain all of the roots, and running load error ct_complex_vec roots; ct_float avg_err = ct_roots(z_next, p, roots); load_err_sum += avg_err; // try to find our root... int root_idx = ct_try_find(z, roots, eps); if (root_idx < 0 || (ct_uint)root_idx >= g_tokens_str.length()) break; std::cout << "loaded[" << i << "]:" << z << "\n"; out_tokens += g_tokens_str[root_idx]; // advance z = z_next; // check for termination condition... if (std::abs(z - z_target) < eps) { std::cout << "fin detected!:[" << i << "]:" << z << "\n"; break; } } // reverse our tokens std::reverse(out_tokens.begin(), out_tokens.end()); out_z = z; return load_err_sum; } int main() { std::cout.precision(ct_float_nlim::max_digits10); std::cout << "g_tokens_str:" << g_tokens_str << "\n\n"; { ct_complex z_origin = { -.75, .06 }; // The original data to be stored std::string stored = "CHRIS"; ct_int power = ct_gain_power(stored); std::cout << "stored:" << stored << "\n"; std::cout << "power:" << power << "\n\n"; std::cout << "________________________________________\n"; // STORE ct_complex z_stored = ct_store(z_origin, power, stored); std::cout << "________________________________________\n"; std::cout << "\nSTORED POINT:" << z_stored << "\n"; std::cout << "________________________________________\n"; // The data loaded from the stored. std::string loaded; ct_complex z_loaded; ct_float eps = .001; // epsilon // LOAD ct_float load_err_sum = ct_load(z_stored, z_origin, power, eps, loaded, z_loaded); std::cout << "________________________________________\n"; std::cout << "\nORIGIN POINT:" << z_origin << "\n"; std::cout << "LOADED POINT:" << z_loaded << "\n"; std::cout << "\nloaded:" << loaded << "\n" "load_err_sum:" << load_err_sum << "\n"; // make sure everything is okay... if (stored == loaded) { std::cout << "\n\nDATA COHERENT! :^D" << "\n"; } else { std::cout << "\n\n***** DATA CORRUPTED!!! Shi%! *****" << "\n"; assert(stored == loaded); } } return 0; }
Optional Paste Settings
Category:
None
Cryptocurrency
Cybersecurity
Fixit
Food
Gaming
Haiku
Help
History
Housing
Jokes
Legal
Money
Movies
Music
Pets
Photo
Science
Software
Source Code
Spirit
Sports
Travel
TV
Writing
Tags:
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
I made $15,000 in 2 days
CSS | 41 min ago | 0.23 KB
This summer smells like money
CSS | 43 min ago | 0.24 KB
API Glitch (Docs Leak)
CSS | 43 min ago | 0.24 KB
FB2600 User Handbook v0.91
1 hour ago | 6.06 KB
FB2600 Administrator & Moderator SOP
2 hours ago | 8.13 KB
Untitled
12 hours ago | 7.38 KB
c2l.puter.site
15 hours ago | 0.48 KB
Looksmaxxing
15 hours ago | 1.83 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!