Giter Club home page Giter Club logo

thesquid.numerics.extensions's Introduction

TheSquid.Numerics.Extensions

GitHub Status NuGet Version

C# implementation of extension methods for BigInteger data type. Such as extracting an Nth root, generating a random value and exponentiation. By Nikolai TheSquid.

I will be glad to merge your pull requests for improve calculation performance. Even if the improvement affects only individual cases from the range of values.

To use these extensions you will need to add to your code following namespaces: System.Numerics and TheSquid.Numerics.Extensions.

NthRootExtension

C# implementation of an extension method to quickly calculate an Nth root (including square root) for BigInteger value.

How to use

Basicly you can copy class NthRootExtension from source repository to your project. Another option is to add the TheSquid.Numerics.Extensions package from the nuget repository to your project's dependencies.

Usage example:

var source = BigInteger.Parse(Console.ReadLine());
var exponent = int.Parse(Console.ReadLine());
var root = source.NthRoot(exponent, out var isExactResult);

How to test

You can start random NthRoot tests right after clone repository and build solution. You must run generate tests and rebuild solution before start speed Nth root tests.

How to understand

The extension method uses two root calculation algorithms: well-known Newton's method and digit-by-digit method. As the degree of the root increases, the calculation by the Newton method slows down, and the digit-by-digit method accelerates. With a root radicand value order of 100,000 decimal digits, the dependence of the calculation speed on the degree of the root is as follows:

root comparison

NextBigIntegerExtension

C# implementation of an extension method to generate random BigInteger value within the specified range.

How to use

Basicly you can copy class NextBigIntegerExtension from source repository to your project. Another option is to add the TheSquid.Numerics.Extensions package from the nuget repository to your project's dependencies.

Usage example:

var min = BigInteger.Parse(Console.ReadLine());
var max = BigInteger.Parse(Console.ReadLine());
var random = new Random(DateTime.Now.Millisecond).NextBigInteger(min, max);

How to test

You can start random tests for NextBigInteger extension method using NextBigIntegerExtensionTests class from project TheSquid.Numerics.Extensions.Tests right after clone repository and build solution.

How to understand

Extension method for the system class Random. Method uses instance of Random class to generate an array of random bytes.

PowCachedExtension

C# implementation of an extension method for faster calculation of powers with repeated parameters using cache.

How to use

Basicly you can copy class PowCachedExtension from source repository to your project. Another option is to add the TheSquid.Numerics.Extensions package from the nuget repository to your project's dependencies.

Usage example:

var source = BigInteger.Parse(Console.ReadLine());
var exponent = int.Parse(Console.ReadLine());
var power = source.PowCached(exponent);

Pow cache clears itself automatically. Firstly, if out of memory error occurs when calculating the power. Then the cache will be cleared completely. Secondly, if the number of elements in the cache reaches the number of int.MaxValue. Then the cache will be cleared by half. Additionally, you can check the number of elements in the cache and clear it manually, leaving a specified number of elements.

Usage example:

var available = PowCachedExtension.ItemsInCache;
var threshold = long.Parse(Console.ReadLine());
if (threshold < available) PowCachedExtension.ShrinkCacheData(threshold);

How to test

You can start random tests for PowCached extension method using PowCachedExtensionTests class from project TheSquid.Numerics.Extensions.Tests right after clone repository and build solution.

How to understand

Acceleration is achieved by memorizing results of computing degrees, as well as memorizing the intermediate results obtained in calculation progress. With random basement values in range from 0 to 1000, random exponent values in range from 0 to 1000 and iterations count up to 2000000, the dependence of the calculation speed on cache filling is as follows:

pow comparison

thesquid.numerics.extensions's People

Contributors

thesquidcombatant avatar

Stargazers

Ryan Scott White avatar  avatar  avatar

Watchers

 avatar

thesquid.numerics.extensions's Issues

Consider removing parameter passing by reference for ease of use in arithmetic expressions.

Consider removing parameter passing by reference for ease of use in arithmetic expressions: here, here and here. And check what about here.

Passing a parameter by reference can provide speedup for large values of type BigInteger. But maybe this does not play a role in this case due to the specifics of using the passed value inside the method.

Canceling parameter passing by reference should not break backward compatibility. But it should add convenience when using extension methods in arithmetic expressions.

Attach an adapter for BigDecimal for extension methods.

It is assumed that it is possible to add extension methods for BigDecimal by converting BigDecimal to BigInteger and reusing extension methods for BigInteger.

We should check what is currently available on the BigDecimal data type in C#, see how other libraries work with it.

If there is nothing suitable, then, perhaps, implement the BigDecimal data type yourself. And add implementations of mathematical methods to this class.

Check the possibility of getting rid of the extra exponentiation at Newton's method end.

Check whether it is possible to modify the implementation for calculating the root using Newton's method so, as to get rid of exponentiation to verify that an accurate result is obtained.

I mean this place: GetRootByNewton. It seems that I came across on the Internet an implementation of calculating the square root using the Newton method with checking for the exact value without raising to a power, but by preliminary adding one to a source value.

Check cache clearing behavior for PowCached method.

First, modify the behavior of the ShrinkCacheData method when the input parameter is less than 0. Give this value a physical meaning or, more likely, throw the corresponding error.

Second, check the behavior of the ShrinkCacheData method when it called in case of an out-of-memory error. To do this, I suggest to build the project for x32 system, to make it more convenient to reach out-of-memory without disabling the pagefile.

Third, consider the need to call the ShrinkCacheData method in case the itemsLifetime variable overflows. This can help return the variable's value to its operating range.

Resolve warnings "No Source Link" and "Non deterministic".

Deal with the "No Source Link" and "Non deterministic" flags when viewing a library on a site in "NuGet Package Explorer". It doesn't seem to affect anything, but it would be nice to get rid of it.

The "No Source Link" sign is displayed, although the "Source repository" link is also displayed. Looks like a bug on the NuGet gallery. We need to look for workarounds to deal with this.

To get rid of the "Non deterministic" sign, it seems that it is enough to set a special parameter during assembly. I'm guessing to find the right options locally and then add them to the repository action script.

Clarify implementation of root weight calculating methods.

Now they produce results not proportional to the execution time, but relative to each other, which seems to be not entirely good.

This makes it difficult to add other calculation methods later.

I propose to change the implementation of weight calculations so that, this methods return the approximate absolute execution time in microseconds.

Consider the possibility of applying fast multiplication algorithms when raising to a power.

Consider the possibility of applying fast multiplication algorithms when [raising to a power](Consider the possibility of applying fast multiplication algorithms when raising to a power. I mean the Karatsuba algorithm in particular.

When I implemented it, I was able to get a noticeable performance increase only when multiplying numbers from 4 MB size or more: 17 seconds with normal multiplication and 15 seconds with Karatsuba multiplication or something like that. For values less than 4 MB there was a slowdown due to additional algorithm overhead.

Consider the possible addition of a method for calculating nth-root by binary search.

It is possible to calculate the nth-root of a natural number using binary search because the nth-root function is monotonic and continuous.

I remember getting a tiny advantage when calculating the nth-root with binary search. This was in a certain small range of values in the vicinity of the inflection point of the functions of digit-by-digit nth-root extraction and well-known Newton`s method.

It is necessary to check, perhaps on a certain range of input parameters it may be more profitable to calculate the nth-root using a binary search.

P.S.: can only be processed after #10.

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.