Skip to content

Commit

Permalink
BigInteger is too slow use double instead
Browse files Browse the repository at this point in the history
Also replaced Math.Log(x, 2) with Math.Log2(x)
  • Loading branch information
ChainsManipulator committed Jan 21, 2025
1 parent a39b563 commit b0f664e
Show file tree
Hide file tree
Showing 19 changed files with 27 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override double Calculate(BinaryIntervalsManager manager, Link link)

int[] intervals = manager.GetIntervals();

double result = intervals.Where(t => t > 0).Sum(t => Math.Log(t, 2));
double result = intervals.Where(t => t > 0).Sum(t => Math.Log2(t));

return intervals.Length == 0 ? 0 : Math.Pow(2, result / intervals.Length);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ public override double Calculate(BinaryIntervalsManager manager, Link link)
currentEntrance = manager.GetFirstAfter(manager.GetFirst(i));
if (link == Link.Start || link == Link.Both)
{
avG += Math.Log(currentEntrance, 2);
avG += Math.Log2(currentEntrance);
}
}
else
{
int nextEntrance = manager.GetFirstAfter(manager.GetFirst(i));
avG += Math.Log(nextEntrance - currentEntrance, 2);
avG += Math.Log2(nextEntrance - currentEntrance);
currentEntrance = nextEntrance;
}
}
}

if (link == Link.End || link == Link.Both)
{
avG += Math.Log(manager.Length - currentEntrance, 2);
avG += Math.Log2(manager.Length - currentEntrance);
}

avG = manager.PairsCount == 0 ? 0 : avG / manager.PairsCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override double Calculate(CongenericSequence sequence)
{
double cuttingLength = new CuttingLength().Calculate(sequence);

return Math.Log(sequence.Length - cuttingLength + 1, 2);
return Math.Log2(sequence.Length - cuttingLength + 1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public double Calculate(CongenericSequence sequence, Link link)
int[] intervals = sequence.GetArrangement(link);
if(intervals.Length == 0) return 0;

BigInteger volume = 1;
foreach(int interval in intervals) volume *= interval;
double depth = 0;
foreach(int interval in intervals) depth += Math.Log2(interval);

return BigInteger.Log(volume, 2);
return depth;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public double Calculate(CongenericSequence sequence, Link link)
{
double mean = new ArithmeticMean().Calculate(sequence, link);

return mean == 0 ? 0 : Math.Log(mean, 2);
return mean == 0 ? 0 : Math.Log2(mean);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public double Calculate(CongenericSequence sequence, Link link)

foreach ((int interval, int nk) in intervalsDictionary)
{
double centeredRemoteness = Math.Log(interval, 2) - gDeltaLog;
double centeredRemoteness = Math.Log2(interval) - gDeltaLog;
result += centeredRemoteness * centeredRemoteness * centeredRemoteness * centeredRemoteness * nk / nj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public double Calculate(CongenericSequence sequence, Link link)

foreach ((int interval, int nk) in intervalsDictionary)
{
double centeredRemoteness = Math.Log(interval, 2) - gDeltaLog;
double centeredRemoteness = Math.Log2(interval) - gDeltaLog;
result += centeredRemoteness * centeredRemoteness * centeredRemoteness * nk / nj;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public double Calculate(CongenericSequence sequence, Link link)

foreach ((int interval, int nk) in intervalsDictionary)
{
double centeredRemoteness = Math.Log(interval, 2) - gDeltaLog;
double centeredRemoteness = Math.Log2(interval) - gDeltaLog;
result += centeredRemoteness * centeredRemoteness * nk / nj;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace Libiada.Core.Core.Characteristics.Calculators.CongenericCalculators;

using System.Numerics;

/// <summary>
/// Volume of sequence.
/// </summary>
Expand All @@ -24,9 +22,9 @@ public double Calculate(CongenericSequence sequence, Link link)
int[] intervals = sequence.GetArrangement(link);
if (intervals.Length == 0) return 1;

BigInteger result = 1;
double result = 1;
foreach (int interval in intervals) result *= interval;

return (double)result;
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public class CuttingLengthVocabularyEntropy : NonLinkableFullCalculator
public override double Calculate(ComposedSequence sequence)
{
double cutLength = new CuttingLength().Calculate(sequence);
return Math.Log(sequence.Length - cutLength + 1, 2);
return Math.Log2(sequence.Length - cutLength + 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public double Calculate(ComposedSequence sequence, Link link)

foreach ((int interval, int nk) in intervalsDictionary)
{
double centeredRemoteness = Math.Log(interval, 2) - g;
double centeredRemoteness = Math.Log2(interval) - g;
result += centeredRemoteness * centeredRemoteness * centeredRemoteness * centeredRemoteness * nk / n;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public double Calculate(ComposedSequence sequence, Link link)

foreach ((int interval, int nk) in intervalsDictionary)
{
double centeredRemoteness = Math.Log(interval, 2) - g;
double centeredRemoteness = Math.Log2(interval) - g;
result += centeredRemoteness * centeredRemoteness * centeredRemoteness * nk / n;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public double Calculate(ComposedSequence sequence, Link link)

foreach ((int interval, int nk) in intervalsDictionary)
{
double centeredRemoteness = Math.Log(interval, 2) - g;
double centeredRemoteness = Math.Log2(interval) - g;
result += centeredRemoteness * centeredRemoteness * nk / n;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace Libiada.Core.Core.Characteristics.Calculators.FullCalculators;

using Libiada.Core.Core.SimpleTypes;
using System.Numerics;

/// <summary>
/// Count of probable sequences that can be generated
Expand All @@ -20,7 +19,7 @@ public class VariationsCount : NonLinkableFullCalculator
/// </returns>
public override double Calculate(ComposedSequence sequence)
{
BigInteger count = 1;
double count = 1;
for (int i = 0; i < sequence.Length; i++)
{
if (sequence[i] is ValuePhantom message)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace Libiada.Core.Core.Characteristics.Calculators.FullCalculators;

using System.Numerics;

/// <summary>
/// Volume of sequence.
/// </summary>
Expand All @@ -23,11 +21,11 @@ public double Calculate(ComposedSequence sequence, Link link)
{
CongenericCalculators.Volume calculator = new();

BigInteger result = 1;
double result = 1;
int alphabetCardinality = sequence.Alphabet.Cardinality;
for (int i = 0; i < alphabetCardinality; i++)
{
result *= (BigInteger)calculator.Calculate(sequence.CongenericSequence(i), link);
result *= calculator.Calculate(sequence.CongenericSequence(i), link);
}

return (double)result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public double GetTaxonsValue(FrequencyDictionary alphabet)
for (int index = 0; index < alphabet.Count; index++)
{
int countT = positions[index].Count;
// TODO: check if this should be Log2
taxons += (Math.Log(countT) * countT) - countT;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ private double Symmetry(FrequencyDictionary alphabet)
for (int index = 0; index < alphabet.Count; index++)
{
int countT = positions[index].Count;
// TODO: check if this should be Log2
taxons += (Math.Log(countT) * countT) - countT;
int arraySize = positions[index].Count;
if (arrayMaxLength < arraySize)
Expand All @@ -105,6 +106,7 @@ private double Symmetry(FrequencyDictionary alphabet)
}
}

// TODO: check if this should be Log2
merons += (Math.Log(countM) * countM) - countM;
countM = 0;
}
Expand Down
2 changes: 2 additions & 0 deletions Libiada.Segmenter/Model/Criterion/CriterionPartialOrlov.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public double TheoryVolume(ComplexSequence sequence, FrequencyDictionary alphabe
}

double z = sequence.Length;
// TODO: check if this should be Log2
double k = 1 / Math.Log(f * z);
double b = (k / f) - 1;
double v = (k * z) - b;
Expand Down Expand Up @@ -145,6 +146,7 @@ public double CalculateB(double k, double f1)
/// </returns>
public double CalculateK(int factFrequency, int length)
{
// TODO: check if this should be Log2
return 1.0 / Math.Log(factFrequency * length);
}

Expand Down
1 change: 1 addition & 0 deletions Libiada.Segmenter/PoemsSegmenter/PartialOrlovCriterion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public double CalculateK()
double maxEntriesNumber = consonancesDictionary.Values.Max();
double Z = calculateZ();
string textWithoutSpaces = text.Replace(" ", "");
// TODO: check if this should be Log2
return 1 / Math.Log(maxEntriesNumber);
}

Expand Down

0 comments on commit b0f664e

Please sign in to comment.