LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  Aggregating negative values (as well as positive values)

    Employee
    Posted 05-04-2010 20:59

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: Iain Sanders

    Can you please advise how to add negative as well as positive values using the Agg (Core) library node or something more appropriate?


  • 2.  RE: Aggregating negative values (as well as positive values)

    Employee
    Posted 05-05-2010 06:23

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: ejones

    If you want to add all the negative values into one total and the positive values into a different total, you could do this.

    if firstInGroup then {
    sum_positives = 0
    sum_negatives = 0
    }
    if x > 0 then sum_positives = sum(x)
    if x < 0 then sum_negatives = sum(x)

    Note that with this syntax, you have to set the sums to zero in your code at the start of each group instead of assuming that the it will be done for you. This could be because the the sum() function is burried inside of an if statement.

    But there is a way to avoid having to explicitly set the sums to zero. Do this by putting the if statement inside of what is getting summed up. This way the Brain will have an easier time knowing what variables to clear at the start of each group for you.

    sum_positives = sum(if x > 0 then x else 0)
    sum_negatives = sum(if x < 0 then x else 0)

    I find this second example easier but there have been times where I have found the first syntax better. But it is also more dangerous because you have to remember to clear the variables used for the sum yourself and if you forget to check the results, you would never know because there will be no error messages to tell you there is a problem.


  • 3.  RE: Aggregating negative values (as well as positive values)

    Employee
    Posted 05-05-2010 06:25

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: timonk

    Iain,
    Could you possibly post a description or example of what it is you are actually trying to do? I am not certain my answer will be in the right context otherwise.

    In the aggregate node, to have variables that either increment or decrement by some value, you need to check them against the builtin agg functions "firstInGroup" and "lastInGroup".

    For example:
    ##
    if firstInGroup then Foo = 10
    else Foo = Foo + 10 #or, you could say else Foo = Foo - 10

    emit *, Foo as "New Foo"
    where lastInGroup
    ##
    In this example, the aggregate variable Foo will be incremented by +10 for each iteration through an aggregate group. If you wanted you could subtract 10 in the same fashion.

    The variable lastInGroup can also be used in a similar fashion to set aggregate variables, or modify their value.

    You can just as simply use input or parameter values.

    if firstInGroup then Foo = (<FIELD ONE VALUE> - <FIELD TWO VALUE> )/{{^a graph parameter^}} + {{^a node parameter^}}


    Regards
    Timon Koufopoulos
    MDA Support.