-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataFrameExtensionsArithmetic.cs
More file actions
142 lines (120 loc) · 5.03 KB
/
DataFrameExtensionsArithmetic.cs
File metadata and controls
142 lines (120 loc) · 5.03 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Linq;
using System.Numerics;
using Microsoft.Data.Analysis;
namespace Dimension.DataFrame.Extensions;
/// <summary>
/// Arithmetic extension methods to make Microsoft's DataFrame a little more user-friendly.
/// </summary>
public static class DataFrameExtensionsArithmetic
{
public static PrimitiveDataFrameColumn<T> Plus<T>(this PrimitiveDataFrameColumn<T> column, PrimitiveDataFrameColumn<T> otherColumn, string name = "")
where T : unmanaged, INumber<T>
{
return column.Plus(name, otherColumn);
}
public static PrimitiveDataFrameColumn<T> Plus<T>(this PrimitiveDataFrameColumn<T> column, string name = "", params PrimitiveDataFrameColumn<T>[] otherColumns)
where T : unmanaged, INumber<T>
{
if (otherColumns.Any(c => c.Length != column.Length))
{
throw new ArgumentException("All columns must have the same length.");
}
var result = new T[column.Length];
for (var i = 0; i < column.Length; i++)
{
var sum = column[i] ?? default;
foreach (var otherColumn in otherColumns)
{
sum += otherColumn[i] ?? default;
}
result[i] = sum;
}
if (string.IsNullOrEmpty(name))
{
var namesToConcat = new[] {column.Name}.Concat(otherColumns.Select(c => c.Name));
name = string.Join("+", namesToConcat);
}
return new PrimitiveDataFrameColumn<T>(name, result);
}
public static PrimitiveDataFrameColumn<T> Minus<T>(this PrimitiveDataFrameColumn<T> column, PrimitiveDataFrameColumn<T> columnToSubtract, string name = "")
where T : unmanaged, INumber<T>
{
if (column.Length != columnToSubtract.Length)
{
throw new ArgumentException("Both columns must have the same length.");
}
var result = new T[column.Length];
for (var i = 0; i < column.Length; i++)
{
var value1 = column[i] ?? default; // Handle nulls as default(T)
var value2 = columnToSubtract[i] ?? default; // Handle nulls as default(T)
result[i] = value1 - value2; // Correct use of subtraction with INumber<T>
}
if (string.IsNullOrEmpty(name))
{
name = $"{column.Name}-{columnToSubtract.Name}";
}
return new PrimitiveDataFrameColumn<T>(name, result);
}
public static PrimitiveDataFrameColumn<T> Times<T>(this PrimitiveDataFrameColumn<T> column, PrimitiveDataFrameColumn<T> columnToMultiplyBy, string name = "")
where T : unmanaged, INumber<T>
{
// Wrapper around the Times method with params
return Times(column, name, columnToMultiplyBy);
}
// Times method for a params array of columns
public static PrimitiveDataFrameColumn<T> Times<T>(this PrimitiveDataFrameColumn<T> column, string name = "", params PrimitiveDataFrameColumn<T>[] otherColumns)
where T : unmanaged, INumber<T>
{
if (otherColumns.Any(c => c.Length != column.Length))
{
throw new ArgumentException("All columns must have the same length.");
}
var result = new T[column.Length];
for (var i = 0; i < column.Length; i++)
{
var product = column[i] ?? default;
foreach (var otherColumn in otherColumns)
{
product *= otherColumn[i] ?? default;
}
result[i] = product;
}
if (string.IsNullOrEmpty(name))
{
var namesToConcat = new[] {column.Name}.Concat(otherColumns.Select(c => c.Name));
name = string.Join("*", namesToConcat);
}
return new PrimitiveDataFrameColumn<T>(name, result);
}
public static PrimitiveDataFrameColumn<T> Divide<T>(this PrimitiveDataFrameColumn<T> numeratorColumn, PrimitiveDataFrameColumn<T> divisorColumn, string name = "")
where T : unmanaged, INumber<T>
{
if (numeratorColumn.Length != divisorColumn.Length)
{
throw new ArgumentException("Both columns must have the same length.");
}
var result = new T[numeratorColumn.Length];
for (var i = 0; i < numeratorColumn.Length; i++)
{
var numerator = numeratorColumn[i] ?? default; // Handle nulls as default(T)
var divisor = divisorColumn[i] ?? default; // Handle nulls as default(T)
if (divisor == T.Zero || divisor == default) // Check for division by zero
{
result[i] = typeof(T) == typeof(float) ? (T) (object) float.NaN :
typeof(T) == typeof(double) ? (T) (object) double.NaN :
default;
}
else
{
result[i] = numerator / divisor; // Perform division
}
}
if (string.IsNullOrEmpty(name))
{
name = $"{numeratorColumn.Name}/{divisorColumn.Name}";
}
return new PrimitiveDataFrameColumn<T>(name, result);
}
}