Giter Club home page Giter Club logo

rationalnumerics's Introduction

BigRational - a novel rational number class.

A new implementation is under development:
BigRat.cs (as single file solution, just copy and try)
New basics, smaller, more performance on operator level, description in short.
Maybe already the better choice.

BigRational is a rational number class for arbitrary-precision arithmetic.
There are no practical limits to the precision except the ones implied by the available memory.
In difference to the usual implementations of such a class, the new system offers a significantly better performance and better memory usage.
This is mainly due to the advantages of a stack machine, which minimizes the need for memory allocations and copies, and which allows to apply more efficient numerical algorithms. The way it works and the Api Documentation is available on GitHub-Pages.

As ultimate speed test compared to an ordinary BigRational class based on BigInteger, here the Mandelbrot set.
This example contains everything, all basic numeric operations, small and big numbers, numbers with few and with many digits, iterations, threading and is generally very computationally intensive.
It shows that the new system is many times faster than what is even possible with conventional technique.
Theoretically, the increase in performance should be around factor of 10, which is confirmed by the test.
By zooming deeper into the Mandelbrot set, a factor of 20 and more is possible due to the massive memory garbage the old system implies.

mandel1

The second example is an ultimate test for the stacking machine.
The Tesselator uses its own instance of a BigRational.CPU stack machine, which is also used as a vertex buffer. Almost all CPU instructions are in use.
In this form no further memory allocations are necessary at runtime and the tessellation can never fail what is a big problem for any floating-point tessellation algorithm.

tess1

The third example is an intensive test for a set of 3D vector types and algorithms based on BigRational and BigRational.CPU arithmetic.
All 3D mesh calculations, polygon tessellations, CSG, etc. are done in real time at animation time.

polyhedron1

The fourth example, a calculator, is intended for testing BigRational's high-level functions API.
No problem to calculate with thousands of decimal digits.
Precisission, string formattings and speed for the several functions can be checked.

calc1

The Benchmarks are currently under construction.
The bottle-nack for rational arbitrary arithmetic is the integer multiplication and for the normalization the GCD function and the integer division.
The first benchmarks showing that the BigRational calculation core is aprximatly 15% faster then the equivalent functions in System.Numerics.BigInteger.
With other words, using BigRagtional for pure integer arithmetics can improve the performance, and together by using the CPU dramatic.

BigInteger in NET 7 will use Spans more stack allocs and shared buffers.
The benchmarks made with NET 7 preview versions are showing that this reduces a little bit the memory pressure
but further degreads the performance especialliy for big numbers.

banch1

More detailed benchmarks coming soon.

rationalnumerics's People

Contributors

c-ohle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

kant2002 ehtick

rationalnumerics's Issues

Samples based on new BigRat

Hi, very impressive project. I was wondering if there is an implementation of the samples (fractal, tesselator, csg etc.) available that uses the new BigRat class? The code using the stack architecture full of push and pop is quite hard to read and understand.

Possible issues may arise when using this code in asynchronous methods

Not sure if you accept suggestions, but found a possible problem when reading the documentation of how works this code.

The exposed property static CPU task_cpu would not work property on asynchronous code.
Imagine the following:

// Thread "1".
BigRational.task_cpu.push(value1);
await SomeOperation();
// Resume on thread "2".
BigRational.task_cpu.push(value2); // Pushing on a different cpu.

The problem also persists when caching the access:

// Thread "1".
CPU cpu= BigRational.task_cpu;
cpu.task_cpu.push(value1);
await SomeOperation();
// Resume on thread "2".
cpu.task_cpu.push(value2); // Pushing on cpu of thread "1" from thread "2", which is not thread safe.

Instead, the [ThreadStatic] static CPU? cpu; field should be static AsyncLocal<CPU?> cpu = new();. This wrapper handles all issues that may raise when suspending or resuming asynchronous code.

However, this would also slow down implemented operators in BigRational, so I would suggest having two properties:

// Only used inside the library for the operators.
private static CPU thread_cpu_ => thread_cpu??= new();
[ThreadStatic]
private static CPU? thread_cpu_;

public static CPU task_cpu => cpu.Value ??= new();
private static AsyncLocal<CPU?> cpu;

BTW, amazing job.

Error on Blazor WASM on version 1.0.0.7

After upgrading to version 1.0.0.7 in the published Blazor WASM application I get this error, while in the development environment it works correctly. If I downgrade to version 1.0.0.6 it works fine.
I can't figure out why since it works in the development environment.

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Arg_TargetInvocationException
System.Reflection.TargetInvocationException: Arg_TargetInvocationException
---> System.TypeLoadException: Could not load list of method overrides due to Method not found: int System.Numerics.INumberBase1.get_Radix() at System.Reflection.ConstructorInvoker.InterpretedInvoke(Object , Span1 , BindingFlags )
Exception_EndOfInnerExceptionStack
at System.RuntimeType.CreateInstanceMono(Boolean , Boolean )
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean , Boolean )
at System.Activator.CreateInstance(Type , Boolean , Boolean )
at System.Activator.CreateInstance(Type , Boolean )
at System.Activator.CreateInstance(Type )
at Microsoft.AspNetCore.Components.DefaultComponentActivator.CreateInstance(Type )
at Microsoft.AspNetCore.Components.ComponentFactory.InstantiateComponent(IServiceProvider , Type )
at Microsoft.AspNetCore.Components.RenderTree.Renderer.InstantiateComponent(Type )
at Microsoft.AspNetCore.Components.RenderTree.Renderer.InstantiateChildComponentOnFrame(RenderTreeFrame& , Int32 )
at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewComponentFrame(DiffContext& , Int32 )
at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewSubtree(DiffContext& , Int32 )
at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InsertNewFrame(DiffContext& , Int32 )
at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext& , Int32 , Int32 , Int32 , Int32 )
at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.ComputeDiff(Renderer , RenderBatchBuilder , Int32 , ArrayRange1 , ArrayRange1 )
at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder , RenderFragment , Exception& )
at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch(RenderQueueEntry )
at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()

.NET 7 NuGet package?

How are things going on your end? Love the trajectory of this package and I hope it gets into .NET in the future!

Exception when open Polyhedron

When opening Polyhedron tab on my PC. On other laptop tab was opening fine.

System.ArgumentException: Value does not fall within the expected range.
   at Test.DX11Ctrl.IDevice.CreatePixelShader(Byte* ShaderBytecode, IntPtr BytecodeLength, Void* ClassLinkage)
   at Test.DX11Ctrl.apply() in c:\RationalNumerics\Test\mini3d\DX11Ctrl.cs:line 388
   at Test.DX11Ctrl.DC.DrawMesh(VertexBuffer vb, IndexBuffer ib, Int32 i, Int32 n) in c:\RationalNumerics\Test\mini3d\DX11Ctrl.cs:line 1223
   at Test.DX11ModelCtrl.render(DC dc, Int32 was, Group[] nodes) in c:\RationalNumerics\Test\mini3d\DX11ModelCtrl.cs:line 86
   at Test.DX11ModelCtrl.OnRender(DC dc) in c:\RationalNumerics\Test\mini3d\DX11ModelCtrl.cs:line 107
   at Test.DX11Ctrl.Refresh() in c:\RationalNumerics\Test\mini3d\DX11Ctrl.cs:line 147
   at Test.DX11Ctrl.WndProc(Message& m) in c:\RationalNumerics\Test\mini3d\DX11Ctrl.cs:line 621
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)

Error BigRational.Pow

Console.WriteLine(BigRational.Pow(BigRational.Parse("2"), BigRational.Parse("1024")).ToString("F5000").Trim('0').Trim('.').Trim(','));

output:
179769313486231590772930519088622001011118896214832765155304275349863344832230481635579222589753905511470408352693059293915016093195361235646204211316618291270358047266252361344772967473693272614986720710237742201778406260548280080727716004012369601250654400739018446628865972481023592362912353548367809468670,72419534065285259505027929807

This answer is incorrect. Right:

179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216

Updating packages?

Hi, first of all this project is awesome!

I've been test-driving the code in F# with .NET 7.0 preview using the BigRational NuGet package. However, it appears that explicit casting to BigInt wasn't added to the project codebase until more recently, so I haven't been able to try out the newer changes. When is the next package expected to be released?

Alternatively, do you happen to know of a way to reference this GitHub repo using Paket? I've tried something like adding the paket add c-ole/RationalNumerics but that didn't seem to work.

Thanks in advance for your help!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.