1 Pages (6 items)
sum of a list or vector that can sometimes have a single value - Messages
#1 Posted: 3 years ago
Hello,
I have tried this function in order to be able to add together all the elements of a list (or vector) of variable length, including having one single element, but I don't know how to make it work.
suml(a):=if IsDefined(length(a))
sum(a)
else
a
It says "syntax is incorrect"...
Any suggestion?
If the boolean testing is "false" then is the "true" option evaluated or not? (I guess it shouldn't...)
Thanks in advance
Laurent
I have tried this function in order to be able to add together all the elements of a list (or vector) of variable length, including having one single element, but I don't know how to make it work.
suml(a):=if IsDefined(length(a))
sum(a)
else
a
It says "syntax is incorrect"...
Any suggestion?
If the boolean testing is "false" then is the "true" option evaluated or not? (I guess it shouldn't...)
Thanks in advance
Laurent
#2 Posted: 3 years ago
#3 Posted: 3 years ago
Hello I found a solution:
suml(a):=line(
a:=stack(a,0)
sum(a)
)
Sorry to have bothered this forum with such a basic question !
suml(a):=line(
a:=stack(a,0)
sum(a)
)
Sorry to have bothered this forum with such a basic question !
WroteHello,
I have tried this function in order to be able to add together all the elements of a list (or vector) of variable length, including having one single element, but I don't know how to make it work.
suml(a):=if IsDefined(length(a))
sum(a)
else
a
It says "syntax is incorrect"...
Any suggestion?
If the boolean testing is "false" then is the "true" option evaluated or not? (I guess it shouldn't...)
Thanks in advance
Laurent
#4 Posted: 3 years ago
#5 Posted: 3 years ago
#6 Posted: 3 years ago
1 Pages (6 items)
-
New Posts
-
No New Posts