All Downloads are FREE. Search and download functionalities are using the official Maven repository.

protelis.lang.utils.pt Maven / Gradle / Ivy

There is a newer version: 17.7.1
Show newest version
module protelis:lang:utils
import java.lang.Math.pow

/**
 * @param a bool, first condition
 * @param b bool, second condition
 * @return  bool, true if both the conditions are true
 */
public def and(a, b) {
    a && b
}

///**
// * Build a field from a value.
// *
// * Return type: (any) -> any.
// * @param value value to be fielded
// * @return field of the given value
// */
//public def field(v) {
//    nbr(v)
//}

/**
 * Line-of-sight detector.
 *
 * @param a T, value
 * @return  bool, true if there is at least a neighbor respect to x, false otherwise
 */
public def canSee(a) {
    anyHood(nbr(a))
}

///**
// * Return type: (fun) -> fun.
// * @param f function
// * @return g(f(args))
// */
//public def composeF(f) {
//    () -> { f.apply() }
//}
//
///**
// * Return type: (any) -> fun.
// * @param a value to be composed as a function
// * @return a function which returns a
// */
//public def composeV(a) {
//    () -> { a }
//}

/**
 * @param x      num, value to be denormalized
 * @param oldmin num, oldmin
 * @param oldmax num, oldmax
 * @param newmin num, newmin
 * @param newmax num, newmax
 * @return       num, denormalized value
 */
public def denormalize(x, oldmin, oldmax, newmin, newmax) {
    x * (newmax - newmin) / (oldmax - oldmin) + (newmin - oldmin)
}

/**
 * @param a num, first field
 * @param b num, second field
 * @return  num, difference between two fields
 */
public def diff(a, b) {
    a - b
}

/**
 * *-hood wrapper.
 *
 * @param local        T, local value
 * @param aggregator   (T, T) -> T, how to reduce information
 * @param default      T, default value in case the field returned by fieldBuilder is empty
 * @param fieldBuilder () -> T, return which field should be reduced
 * @return             T, reduced value
 */
public def hoodW(local, aggregator, default, fieldBuilder) {
    aggregator.apply(local,
        hood(
            (a, b) -> { aggregator.apply(a, b) },
            default,
            fieldBuilder.apply()
        )
    )
}

/**
 * Stateful *-hood wrapper.
 *
 * @param local        T, local value
 * @param aggregator   (T, T) -> T, how to reduce information
 * @param default      T, default value in case the field returned by fieldBuilder is empty
 * @param fieldBuilder (T) -> T, return which field should be reduced
 * @return             T, reduced value
 */
public def hoodWstateful(local, aggregator, default, fieldBuilder) {
     rep (v <- local) {
         aggregator.apply(local,
             hood(
                 (a, b) -> { aggregator.apply(a, b) },
                 default,
                 fieldBuilder.apply(v)
             )
         )
     }
}

/**
 * @param a T, value to be returned
 * @return  T, the same value
 */
public def identity(a) {
    a
}

/**
 * Verify if a device is on the edge of a spatial region.
 *
 * @param condition bool, how to discriminate a region
 * @return          bool, true if the device is on the edge, false otherwise
 */
 public def isEdge(condition) {
     !(allHood(nbr(condition)) || allHood(!nbr(condition)))
 }

/**
 * @param a T, first field
 * @param b T, second field
 * @return  T, maximum between the two fields
 */
public def max(a, b) {
    if (a.compareTo(b) > 0 ) {
        a
    } else {
        b
    }
}

/**
 * @param a num, first field
 * @param b num, second field
 * @return  num, mean of the two fields
 */
public def mean(local, field) {
    (local + field) / 2
}

/**
 * @param a num, first field
 * @param b num, second field
 * @return  num, minimum between the two fields
 */
public def min(a, b) {
    if (a.compareTo(b) < 0 ) {
        a
    } else {
        b
    }
}

/**
 * Norm of a numeric tuple.
 *
 * @param tuple [num], tuple
 * @param p     num, pow
 * @return      num, norm of a numeric tuple
 */
public def norm(tuple, p) {
    pow(
        tuple
            .map(self, (a) -> { pow(a, p) })
            .reduce(self, 0, (a, b) -> { a + b }),
        1/p
    )
}

/**
 * @param a bool, first condition
 * @return  bool, negated condition
 */
public def not(a) {
    !a
}

/**
 * @param a bool, first condition
 * @param b bool, second condition
 * @return  bool, true if one of the conditions is true
 */
public def or(a, b) {
    a || b
}

/**
 * @param x   num, value
 * @param min num, lower bound
 * @param max num, upper bound
 * @return    num, scale x with respect to the range
 */
public def range(x, min, max) {
    x * (max - min) + min
}

/**
 * @param a num, summand
 * @param b num, summand
 * @return  num, sum of two fields
 */
public def sum(a, b) {
    a + b
}

/**
 * @return num, 0
 */
public def zero() {
    0
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy