Exemple #1
0
def intf_LMUL(E):
    """List multiplication operator where lists are the focus. Compare to `*` where
    items are the focus. All SYMs are resolved to their referent. The input
    requires only that v1 or v2 or both must resolve to a VAL. All other
    combinations are valid. A negative VAL as the multiplier will reverse the
    order of mulitplied lists.

        L V :* -> replication of list items in list, commutative
        T V :* -> put text item in list with copies, commutative
        V V :* -> put v2 in list with v1 copies, not commutative

    Examples:

        [1 2 3] 2 :* -> [1 2 3 1 2 3]
        2 [1 2 3] :* -> [1 2 3 1 2 3]
        ''abc'' 2 :* -> [''abc''  ''abc'']
        4 3 :* -> [4 4 4]
        3 4 :* -> [3 3 3 3]
        [0 1] -3 :* -> [1 0 1 0 1 0]
    """
    if not (inc.VAL(E, 1) or inc.VAL(E, 2)):
        print("Input Error: lmul")
        print(intf_LMUL.__doc__)
        return  # Without doing much of anything.
    v2 = E.resolve_symbol(E.The.StackPop())
    v1 = E.resolve_symbol(E.The.StackPop())
    if not v2.whatami == 'VAL':
        v1, v2 = v2, v1
    if v1.whatami == 'LST':
        outlist = v1.val * abs(int(v2.val))
    else:  # Both are vals.
        outlist = [v1] * abs(int(v2.val))
    if v2.val < 0:
        outlist = outlist[::-1]
    E.The.StackPush(objectifier.StackOB_LST(outlist))
Exemple #2
0
def intf_DISTWEIBULL(E):
    """Takes VALs from v1 and v2, and returns a value randomly generated
    by a function that produces values which are distributed in a Weibull
    distribution. This is often used to model processes related to failure
    rates, survival analysis, and reliability. The v2 VAL is Lambda which is
    the scale parameter for this kind of distribution. The v1 VAL is k which is
    the shape parameter. Both must be greater than zero. If k is 1 this is
    equivalent to the exponential distribution. If k is 2 it is equivalent to
    the Rayleigh distribution. A value of k < 1 indicates that the failure rate
    decreases over time. This happens if there is significant "infant
    mortality", or defective items failing early and the failure rate
    decreasing over time as the defective items are weeded out of the
    population. A value of k = 1 indicates that the failure rate is constant
    over time. This might suggest random external events are causing mortality,
    or failure. A value of k > 1 indicates that the failure rate increases with
    time. This happens if there is an "aging" process, or parts that are more
    likely to fail as time goes on.
        |[.75 1 distweibull::!] 100000 repeat 100000 2list mean -> 0.751247734665
    """
    if not inc.VAL(E, 1) or not inc.VAL(E, 2):
        print("Input Error: distweibull")
        print(intf_DISTWEIBULL.__doc__)
        return  # Without doing much of anything.
    v1 = E.The.StackPop().val  # k, beta, shape
    v2 = E.The.StackPop().val  # Lambda, alpha, scale
    import random
    out = random.weibullvariate(v2, v1)
    out = objectifier.StackOB_VAL(out)
    E.The.StackPush(out)
Exemple #3
0
def intf_PERM(E):
    """Number of possible permutations of v2 items taken v1 at a time.
       fact(v2) / fact(v2-v1)
    """
    if not inc.VAL(E, 1) or not inc.VAL(E, 2):
        print("Input Error: perm")
        print(intf_PERM.__doc__)
        return  # Without doing much of anything.
    v1 = int(E.The.StackPop().val)  # Taken this many at a time.
    v2 = int(E.The.StackPop().val)  # Combination of this many items.
    d = v2 - v1
    out = fact(v2) / fact(d)
    out = objectifier.StackOB_VAL(out)
    E.The.StackPush(out)
Exemple #4
0
def intf_GETN(E):
    """Returns from v2 LST object the value of the list component
    whose position is specified by v1 where v1 is a name from a named list.
    This can be specified as a SYM or TXT value. The alias is `>>`.
    `[1.1 2.2 3.3 4.4::x y z w] |z getn -> 3.3`
    `[1.1 2.2 3.3 4.4::x y z w] ''y'' >> -> 2.2`
    """
    if not inc.list_and_list_position(E,2,1):
        print("Input Error: getn")
        print(intf_GETN.__doc__)
        return # Without doing much of anything.
    if inc.VAL(E,1): # If a number is tried, hand over to GET.
        intf_GET(E)
        return
    n= E.The.StackPop().val # Name
    lstob= E.The.StackPop() # List or text.
    out= None
    if lstob.whatami == "SYM":
        symkey= lstob.val
        lstob= E.symtab[symkey]
        if n in lstob.names:
            out= lstob.val[lstob.names.index(n)]
    else: # Not a SYM, just a LST.
        out= lstob.named_item(n)
    if out is not None:
        E.The.StackPush(out)
Exemple #5
0
def intf_PUTN(E):
    """Takes list from v3 and a position from v2 and replaces the v3
    item at v2 with the value of v1. This should also take a SYM of a
    list. If that is the case, the output should be nothing but the SYM
    should contain the updated list. If a LST is supplied as v3, then an
    updated list is returned."""
    if not inc.list_and_list_position(E,3,2):
        print("Input Error: putn")
        print(intf_PUTN.__doc__)
        return # Without doing much of anything.
    if inc.VAL(E,2): # If a number is tried, hand over to PUT.
        intf_PUT(E)
        return
    newob= E.The.StackPop() # New object to insert.
    n= E.The.StackPop().val # Name
    lstob= E.The.StackPop() # List or text.
    symonly= False
    if lstob.whatami == "SYM":
        symkey= lstob.val
        lstob= E.symtab[symkey]
        symonly= True
    if n in lstob.names:
        lstob.val[lstob.names.index(n)]= newob
    if symonly:
        E.symtab[symkey]= lstob
    else:
        E.The.StackPush(lstob)
Exemple #6
0
def intf_PUT(E):
    """Takes list from v3 and a position from v2 and replaces the v3
    item at v2 with the value of v1. This should also take a SYM of a
    list. If that is the case, the output should be nothing but the SYM
    should contain the updated list. If a LST is supplied as v3, then an
    updated list is returned."""
    if not inc.VAL(E,2) or not inc.listlike(E,3):
        print("Input Error: put")
        print(intf_PUT.__doc__)
        return # Without doing much of anything.
    ob1= E.The.StackPop() # List or text.
    n= int(E.The.StackPop().val) # Position.
    ob3= E.The.StackPop() # List or text.
    oblen= len(ob3.val)
    if oblen < n:
        n= oblen-1
    elif n < 1:
        n= 0
    else:
        n-= 1
    if ob3.whatami == "TXT":
        listified= list(ob3.val) # Python strings are immutable.
        listified[n]= ob1.val
        ob3= objectifier.StackOB_TXT( ''.join(listified) )
    else:
        if ob3.whatami == "SYM":
            ref2= E.resolve_symbol(ob3)
            if hasattr(ref2,'whatami') and ref2.whatami == 'LST':
                ref2.val[n]= ob1
            else:
                print("Confusion.")
        else:
            ob3.val[n]= ob1
    E.The.StackPush(ob3)
Exemple #7
0
def intf_DISTGAUSS(E):
    """Takes VALs from v1 and v2 and returns a value randomly generated by a
    function that produces values which are distributed in a Gaussian
    distribution. The v2 VAL is the mean of the distribution. The v1 VAL is
    the distribution's standard deviation.
        |[.5 .2 distgauss::!] 100000 repeat 100000 2list dup mean swap sdev ->
        0.500337044014 0.200301690936
    """
    if not inc.VAL(E, 1) or not inc.VAL(E, 2):
        print("Input Error: distgauss")
        print(intf_DISTGAUSS.__doc__)
        return  # Without doing much of anything.
    v1 = E.The.StackPop().val  # Standard deviation.
    v2 = E.The.StackPop().val  # Mean.
    import random
    out = random.gauss(v2, v1)
    out = objectifier.StackOB_VAL(out)
    E.The.StackPush(out)
Exemple #8
0
def intf_TAILN(E):
    """Returns a list or substring made of the first v1 items of a
    list or string (respectively) at v2."""
    if not inc.VAL(E,1) or not inc.listlike(E,2):
        print("Input Error: tailn")
        print(intf_TAILN.__doc__)
        return # Without doing much of anything.
    n= int(E.The.StackPop().val) # Position.
    ob2= E.The.StackPop() # List or text.
    if ob2.whatami == "TXT":
        out= objectifier.StackOB_TXT( ob2.val[-n:] )
    else:
        out= objectifier.StackOB_LST( ob2.val[-n:] )
    E.The.StackPush(out)
Exemple #9
0
def intf_SUB(E):
    """Substring or sublist. Takes a string or list object from v3 and a start
    position from v2 and an end position from v1 and returns the sub-string or
    sub-list (respectively) from v2 to v1. If v1 is less than v2, an empty
    string or list is returned. Any positional value less than 1 is treated as
    1. Any positional value greater than the `len` of v3 will be treated as the
    `len` of v3. The positional values are inclusive such that if v2 is 2 and
    v3 is 4, both the 2nd (and 3rd) and 4th position items will be part of the
    returned sub-object. 
        ''abcdefghijklmnopqrstuvwxyz'' 19 21 sub -> ''stu''
    """
    if not inc.VAL(E,1) or not inc.VAL(E,2) or not inc.listlike(E,3):
        print("Input Error: sub")
        print(intf_SUB.__doc__)
        return # Without doing much of anything.
    n2= int(E.The.StackPop().val) # End position.
    n1= int(E.The.StackPop().val) # Start position.
    ob3= E.The.StackPop() # List or text.
    oblen= len(ob3.val)
    if oblen < n1:
        n1= oblen-1
    elif n1 < 1:
        n1= 0
    else:
        n1-= 1
    if oblen < n2:
        n2= oblen
    elif n2 < 1:
        n2= 0
    else:
        pass # Ok.
    out= ob3.val[n1:n2]
    if ob3.whatami == "LST":
        out= objectifier.StackOB_LST( out )
    elif ob3.whatami == "TXT":
        out= objectifier.StackOB_TXT( out )
    E.The.StackPush(out)
Exemple #10
0
def intf_DISTEXP(E):
    """Takes VAL from v1 and returns a value randomly generated by a
    function that produces values which are distributed in an exponential 
    distribution. The v1 VAL is Lambda which is a parameter of this kind
    of distribution. The formula is `f(x,Lambda)= Lambda * exp(-Lambda * x)`.
    The mean is 1/Lambda and the variance is 1/(Lambda*Lambda).
        |[.75 distexp::!] 100000 repeat 100000 2list 
        dup mean inv swap var inv sqrt -> 0.743701781531 0.742319134654
    """
    if not inc.VAL(E, 1):
        print("Input Error: distexp")
        print(intf_DISTEXP.__doc__)
        return  # Without doing much of anything.
    v1 = E.The.StackPop(
    ).val  # Lambda of the distribution, not Python's lambda.
    import random
    out = random.expovariate(v1)
    out = objectifier.StackOB_VAL(out)
    E.The.StackPush(out)
Exemple #11
0
def intf_REPL(E):
    """Takes a string or list object from v3 and a position from v2
    and a replacement string or list object from v1 and replaces the
    portion of v3 starting at v2 with v1. If v2 is less than 1 or
    greater than the `len` of v3, the items are simply concatenated,
    v1+v3 and v3+v1 respectively.
       [5 10 16 21 25] 3 [15 20] -> [5 10 15 20 25]
       ''abcdefghi'' 4 ''XYZ'' repl -> ''abcXYZghi''
    """
    if ( ( not inc.VAL(E,2) or not inc.listlike(E,1) or not inc.listlike(E,3) )
        or ( inc.TXT(E,1) != inc.TXT(E,3) ) ): # If types don't match.
            print("Input Error: repl")
            print(intf_REPL.__doc__)
            return # Without doing much of anything.
    ob1= E.The.StackPop() # Replacement text or list
    n= int(E.The.StackPop().val) # Position.
    ob3ob= E.The.StackPop() # Original list or text.
    outistxt= (ob3ob.val == "TXT")
    oblen= len(ob3ob.val)
    if oblen < n:
        ob3= ob3ob.val+ob1.val
    elif n < 1:
        ob3= ob1.val+ob3ob.val
    else:
        n-= 1
        if outistxt:
            ob3= list(ob3ob.val) # Python strings are immutable.
        else:
            ob3= ob3ob.val
        start= ob3ob.val[0:n]
        plusmid= start + ob1.val
        ob3= plusmid
        if len(plusmid) < oblen:
            ob3+= ob3ob.val[len(plusmid):]
    if outistxt:
        outob= objectifier.StackOB_TXT( ''.join(plusmid) )
    else:
        outob= objectifier.StackOB_LST( ob3 )
    E.The.StackPush(outob)
Exemple #12
0
def intf_GET(E):
    """Returns from v2 LST object the value of the list component
    whose position is specified by v1. `[1.1 2.2 3.3 4.4] 3 get -> 3.3`
    See GETN for specifying position by list name.
    """
    if not inc.VAL(E,1) or not inc.listlike(E,2):
        print("Input Error: get")
        print(intf_GET.__doc__)
        return # Without doing much of anything.
    n= int(E.The.StackPop().val) # Position.
    ob2= E.The.StackPop() # List or text.
    oblen= len(ob2.val)
    if oblen < n:
        n= oblen-1
    elif n < 1:
        n= 0
    else:
        n-= 1
    if ob2.whatami == "TXT":
        out= objectifier.StackOB_TXT( ob2.val[n] )
    else:
        out= objectifier.StackOB_LST( ob2.val[n] )
    E.The.StackPush(out)
Exemple #13
0
def intf_MUL(E):
    """Item multiplication operator where the values are the focus. Compare to
    `:*` where lists are the focus. Valid inputs:
      SYM SYM   + -> (resolve any SYMs and proceed again) 
      VAL VAL * -> simple multiplication
      TXT VAL * -> replication of text into concatenated text, commutative input
      LST_of_VALs VAL * ->  list of each list item multiplied by VAL, commutative input
      LST_of_VALs LST_of_VALs * -> pad smaller with 1 and multiply each ith pos
    If two lists are multiplied, the shorter one is padded with ones to match the other.
    In the case of two lists and both have list names, the longest list's are used. 
    Examples:
      2 2 * -> 4
      2 [1 2 3] * -> [2 4 6]
      31.831 |diameter sto |diameter |pi * -> 100.000035756
      [pi pi pi] pi inv * -> [1 1 1]
      [1 2 3] 2 * -> [2 4 6]
      [1 2 3] [1 2] * -> [1 4 3]  Pad the shorter list with ones.
      [2 2] [4 4 4::x y z] * -> [8 8 4]<x y z>
      [1 2] [1 2 3] * -> [1 4 3]
      4 ''ha'' * -> ''hahahaha''
      ''backwards'' -2 * -> ''sdrawkcabsdrawkcab''
    """
    if not ((inc.TXT(E, 1) and inc.VAL(E, 2)) or
            (inc.VAL(E, 1) and inc.TXT(E, 2)) or
            ((inc.VAL(E, 1) or inc.LST_of_VALs(E, 1)) and
             (inc.VAL(E, 2) or inc.LST_of_VALs(E, 2)))):
        print("Input Error: mul")
        print(intf_MUL.__doc__)
        return  # Without doing much of anything.
    v2 = E.resolve_symbol(E.The.StackPop())
    v1 = E.resolve_symbol(E.The.StackPop())
    if v1.whatami == 'VAL' and v2.whatami == 'VAL':
        E.The.StackPush(objectifier.StackOB_VAL(v1.val * v2.val))
    elif v1.whatami == "TXT":
        if v2.val < 0:
            v1.val, v2.val = v1.val[::-1], v2.val * -1  # Do this for `neg`.
        E.The.StackPush(objectifier.StackOB_TXT(v1.val * int(v2.val)))
    elif v2.whatami == "TXT":
        if v1.val < 0:
            v2.val, v1.val = v2.val[::-1], v1.val * -1  # It's silly, I know.
        E.The.StackPush(objectifier.StackOB_TXT(v2.val * int(v1.val)))
    elif ((v1.whatami == 'LST' and v2.whatami == 'VAL')
          or (v1.whatami == 'VAL' and v2.whatami == 'LST')):  # Mixed LST&VAL
        if v1.whatami == 'VAL': v1, v2 = v2, v1  # Ensure LST 1st then VAL 2nd
        outlist = list()
        for i in v1.val:
            if i.whatami == 'SYM':
                i = E.resolve_symbol(i)
            outlist.append(objectifier.StackOB_VAL(i.val * v2.val))
        outlistob = objectifier.StackOB_LST(outlist)
        outlistob.names = v1.names[:]
        E.The.StackPush(outlistob)
    elif v1.whatami == 'LST' and v2.whatami == 'LST':
        lv1, lv2 = len(v1.val), len(v2.val)
        if lv1 < lv2:
            v1, v2 = v2, v1  # Longest LST first.
            lv1, lv2 = lv2, lv1
        outlist = list()
        for n, i in enumerate(v1.val):
            if i.whatami == 'SYM':
                i = E.resolve_symbol(i)
            if n < lv2:
                if v2.val[n].whatami == 'SYM':
                    froml2 = E.resolve_symbol(i)
                else:
                    froml2 = v2.val[n]
                outlist.append(objectifier.StackOB_VAL(i.val * froml2.val))
            else:
                outlist.append(objectifier.StackOB_VAL(i.val))
        outlistob = objectifier.StackOB_LST(outlist)
        if not v1.names and v2.names:  # If both have names go with v1's.
            v1, v2 = v2, v1
        outlistob.names = v1.names[:]
        E.The.StackPush(outlistob)
    else:
        print(
            "Error: What the hell have you done!? This should never have happend. See `intf_MUL`."
        )
    E.Undo.StackPush([objectifier.StackOB_SYM('drop'), v2, v1])
Exemple #14
0
def intf_ADD(E):
    """Item addition operator where the values are the focus. Compare 
    to `:+` where lists are the focus. All SYMs are resolved. Valid inputs:
        TXT TXT + -> concatenate text
        VAL VAL + -> simple addition, commutative
        LST_of_VALs VAL + -> add VAL to all items in list (of values), commutative
        LST_of_VALs LST_of_VALs + -> pad smaller with zero and add each ith pos, commutative
    If two lists are added, the shorter one is padded with zeros to match the other.
    In the case of two lists and both have list names, the longest list's are used. 
    Examples:
        2 2 + -> 4
        |pi pi + 2deg -> 360
        [pi 10::a b] |Q sto -180 2rad Q.a + -> 0
        [4 8] 3 + -> [7 11]
        [4 8] [2 3] + -> [6 11]
        [4 8] [2 3] + -> [6 11]
        [4 8 3] [2 3] + -> [6 11 3]
        [4 8] [1 2 3] + -> [5 10 3] 
        [0 4] [1 2 3] + -> [1 6 3] 
        [1 2 3::a b c] [2 2 2::x y z] + -> [3 4 5]<a b c>
        ''xed'' ''.ch'' + -> ''xed.ch''
    """
    if not ((inc.TXT(E, 1) and inc.TXT(E, 2)) or
            ((inc.VAL(E, 1) or inc.LST_of_VALs(E, 1)) and
             (inc.VAL(E, 2) or inc.LST_of_VALs(E, 2)))):
        print("Input Error: add")
        print(inc.LST_of_VALs(E, 1))
        print(inc.LST_of_VALs(E, 2))
        print(" Value 1:" + str(E.The.StackPop()))
        print(" Value 2:" + str(E.The.StackPop()))
        print(intf_ADD.__doc__)
        return  # Without doing much of anything.
    v2 = E.resolve_symbol(E.The.StackPop())
    v1 = E.resolve_symbol(E.The.StackPop())
    if v1.whatami == 'TXT':  # v2's been checked.
        E.The.StackPush(objectifier.StackOB_TXT(v1.val + v2.val))
    elif v1.whatami == 'VAL' and v2.whatami == 'VAL':
        E.The.StackPush(objectifier.StackOB_VAL(v1.val + v2.val))
    elif ((v1.whatami == 'LST' and v2.whatami == 'VAL')
          or (v1.whatami == 'VAL' and v2.whatami == 'LST')):  # Mixed LST&VAL
        if v1.whatami == 'VAL': v1, v2 = v2, v1  # Ensure LST 1st then VAL 2nd
        outlist = list()
        for i in v1.val:
            if i.whatami == 'SYM':
                i = E.resolve_symbol(i)
            outlist.append(objectifier.StackOB_VAL(i.val + v2.val))
        outlistob = objectifier.StackOB_LST(outlist)
        outlistob.names = v1.names[:]
        E.The.StackPush(outlistob)
    elif v1.whatami == 'LST' and v2.whatami == 'LST':
        lv1, lv2 = len(v1.val), len(v2.val)
        if lv1 < lv2:
            v1, v2 = v2, v1  # Longest LST first.
            lv1, lv2 = lv2, lv1
        outlist = list()
        for n, i in enumerate(v1.val):
            if i.whatami == 'SYM':
                i = E.resolve_symbol(i)
            if n < lv2:
                if v2.val[n].whatami == 'SYM':
                    froml2 = E.resolve_symbol(i)
                else:
                    froml2 = v2.val[n]
                outlist.append(objectifier.StackOB_VAL(i.val + froml2.val))
            else:
                outlist.append(objectifier.StackOB_VAL(i.val))
        outlistob = objectifier.StackOB_LST(outlist)
        if not v1.names and v2.names:  # If both have names go with v1's.
            v1, v2 = v2, v1
        outlistob.names = v1.names[:]
        E.The.StackPush(outlistob)
    else:
        print(
            "Error: What the hell have you done!? This should never have happend. See `intf_ADD`."
        )
    E.Undo.StackPush([objectifier.StackOB_SYM('drop'), v2, v1])