You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
proccumsum[T: SomeFloat](p: Tensor[T]): Tensor[T] {.noInit.} =## Calculates the cumulative sum of a vector.## Inputs:## - p: a rank-1 tensor to cumulatively sum#### TODO: implement parallel prefix sum## See: https://en.wikipedia.org/wiki/Prefix_sum#Algorithm_2:_Work-efficient#### Returns:## - A tensor cumulatively summed, that is, add each value to## all previous values, sequentiallyresult= p.clone()
assert p.rank ==1assert p.shape[1] ==0let n_rows = p.shape[0]
for i in1..<n_rows:
result[i] +=result[i-1]
Generalize this procedure to work on n-dimensional Tensors.
Create a procedure to do cumulative product cumprod that is similar to cumsum, but instead of sum, use prod.
The text was updated successfully, but these errors were encountered:
@HugoGranstrom suggested this proc cumprod at the discord:
proccumsum*[T](t: Tensor[T], axis: int): Tensor[T] =## Calculates the cumulative sum of a rank-n Tensor.## Inputs:## - t: a rank-n tensor to cumulatively sum## - axis: int## Returns:## - A tensor cumulatively summed at axisresult=zeros_like(t)
for i, tAxis inenumerateAxis(t, axis):
var temp =result.atAxisIndex(axis, i)
if i ==0:
temp[_] = tAxis
else:
temp[_] =result.atAxisIndex(axis, i-1) + tAxis
Then I suggested to create the cumprod as:
proccumprod*[T](t: Tensor[T], axis:int): Tensor[T] =# from hugogranstrom## Calculates the cumulative product of a rank-n Tensor.## Inputs:## - t: a rank-n tensor to cumulatively sum## - axis: int## Returns:## - A tensor cumulatively product at axis, that is, product each valueresult=zeros_like(t)
for i, tAxis inenumerateAxis(t, axis):
var temp =result.atAxisIndex(axis, i)
if i ==0:
temp[_] = tAxis
else:
temp[_] =result.atAxisIndex(axis, i-1) *. tAxis
I think these are useful for numerous tasks in scientific computing and finance.
I have three suggestions:
cumsum
procedure:Arraymancer/src/arraymancer/ml/clustering/kmeans.nim
Line 26 in 1a2422a
Generalize this procedure to work on n-dimensional Tensors.
Create a procedure to do cumulative product
cumprod
that is similar tocumsum
, but instead of sum, use prod.The text was updated successfully, but these errors were encountered: