Select to view content in your preferred language

cumsum of CGA array

586
3
Jump to solution
02-21-2023 11:39 PM
MagnusLundevall
New Contributor II

How can I get "cumsum" of an array in CGA? Iterate and build a new array recursively in some way? 

0 Kudos
1 Solution

Accepted Solutions
JonasObertuefer
Esri Contributor

Hi @MagnusLundevall,

Yes exactly, here is an example:

 

cumsum(array) = cumsum(array, 1)
cumsum(array, i) =
	case size(array) <= i: array
	else:
		cumsum(setElems(array, i, array[i]+array[i-1]), i+1)
		
Init -->
	print(cumsum([2,5,1,3.5,8]))

 

May I ask about the usecase? If it is something common we might add it as builtin function in the future, so you don't have to rely on recursion for this.

Cheers,
Jonas

View solution in original post

0 Kudos
3 Replies
JonasObertuefer
Esri Contributor

Hi @MagnusLundevall,

Yes exactly, here is an example:

 

cumsum(array) = cumsum(array, 1)
cumsum(array, i) =
	case size(array) <= i: array
	else:
		cumsum(setElems(array, i, array[i]+array[i-1]), i+1)
		
Init -->
	print(cumsum([2,5,1,3.5,8]))

 

May I ask about the usecase? If it is something common we might add it as builtin function in the future, so you don't have to rely on recursion for this.

Cheers,
Jonas

0 Kudos
MagnusLundevall
New Contributor II

Thanks! I have used it in trying to solve Split face into rectangular pieces - Esri Community
I extract scope.sx for each face edge in an array and want to process it to compute split x's. 
I also had the need for min(array) and max(array) that return the smallest and largest element, respectively. I did write them in the same recursive manner. It would be great to have these in CGA.

0 Kudos
JonasObertuefer
Esri Contributor

I see. Thanks for the informations.

For min/max of an array there is currently no dedicated function, but you can use sortIndices(), there is an example for this case in the cga reference: https://doc.arcgis.com/en/cityengine/latest/cga/cga-sort-indices-function.htm