53 lines
1.8 KiB
C#
53 lines
1.8 KiB
C#
using System;
|
|
using EvoCalculator.Core.Models.Calculation.Models.Prepared;
|
|
using TridentGoalSeek;
|
|
|
|
namespace EvoCalculator.Core.Calculation.Columns
|
|
{
|
|
public class SumColumn : BaseColumnWithXIRR, IGoalSeekAlgorithm
|
|
{
|
|
private readonly PercentPaymentColumn _percentPaymentColumn;
|
|
private readonly PreparedValues _preparedValues;
|
|
|
|
public SumColumn(int count, DateTempColumn dateTempColumn
|
|
, PreparedValues preparedValues
|
|
, PercentPaymentColumn percentPaymentColumn) : base(count, dateTempColumn)
|
|
{
|
|
_preparedValues = preparedValues;
|
|
_percentPaymentColumn = percentPaymentColumn;
|
|
}
|
|
|
|
public decimal Calculate(decimal inputVariable)
|
|
{
|
|
var x = Convert.ToDouble(inputVariable);
|
|
ComputeValues(x);
|
|
return Convert.ToDecimal(IRR);
|
|
}
|
|
|
|
public void ComputeValues(double x)
|
|
{
|
|
Values[0] = -_preparedValues.BaseCost;
|
|
Values[1] = _preparedValues.FirstPaymentSum;
|
|
|
|
for (var i = 2; i < Values.Length - 1; i++) Values[i] = x * _percentPaymentColumn.Values[i] / 100;
|
|
|
|
Values[^1] = _preparedValues.LastPaymentSum;
|
|
}
|
|
|
|
public void ComputeValues(decimal requiredIRR)
|
|
{
|
|
var goalSeek = new GoalSeek(this);
|
|
goalSeek.SeekResult(requiredIRR,
|
|
new GoalSeekOptions(
|
|
startingStabPoint: Convert.ToDecimal(
|
|
(_preparedValues.BaseCost - _preparedValues.FirstPaymentSum) /
|
|
_preparedValues.Nmper)
|
|
, tineExplorePercentage: 10
|
|
// , maximumAttempts: 10000
|
|
// , initialTineSpacing: 1
|
|
// , focusPercentage: 100
|
|
// , trimFinalInputValue: true
|
|
));
|
|
}
|
|
}
|
|
} |