-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Varp.cs
62 lines (52 loc) · 1.86 KB
/
Varp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) Dmytro Kyshchenko. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
namespace xFunc.Maths.Expressions.Statistical;
/// <summary>
/// Represents the VARP function.
/// </summary>
/// <seealso cref="DifferentParametersExpression" />
public class Varp : StatisticalExpression
{
/// <summary>
/// Initializes a new instance of the <see cref="Varp"/> class.
/// </summary>
/// <param name="arguments">The arguments.</param>
public Varp(IEnumerable<IExpression> arguments)
: base(arguments)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Varp"/> class.
/// </summary>
/// <param name="arguments">The arguments.</param>
public Varp(ImmutableArray<IExpression> arguments)
: base(arguments)
{
}
/// <inheritdoc />
protected override object ExecuteInternal(VectorValue vector)
{
var avg = vector.Average();
var sum = NumberValue.Zero;
foreach (var number in vector)
{
// because power = 2, we can cast directly to `NumberValue`
sum += (NumberValue)NumberValue.Pow(number - avg, NumberValue.Two);
}
return sum / vector.Size;
}
/// <inheritdoc />
protected override TResult AnalyzeInternal<TResult>(IAnalyzer<TResult> analyzer)
=> analyzer.Analyze(this);
/// <inheritdoc />
[ExcludeFromCodeCoverage]
protected override TResult AnalyzeInternal<TResult, TContext>(
IAnalyzer<TResult, TContext> analyzer,
TContext context)
=> analyzer.Analyze(this, context);
/// <inheritdoc />
public override IExpression Clone(ImmutableArray<IExpression>? arguments = null)
=> new Varp(arguments ?? Arguments);
}