def cascade(): sm1 = sm.Delay(1) sm2 = PureFunction(mult) c = Cascade(sm1, sm2) o = c.transduce([1, 3, 7, 2]) return o
def accumulatorDelay(init): """ Args: init (int): the output at time 0 Returns: y: a state machine whose output at time n is the sum of its inputs up to and including time n-1 """ xDelay = sm.Delay(0) y = sm.Cascade(xDelay, accumulator(init)) return y
def accumulator(init): """ Args: init (int): the output at time 0 Returns: y: a state machine whose output at time n is the sum of its inputs up to and including time n """ wire = sm.Gain(1) yDelay = sm.Delay(init) y = sm.FeedbackAdd(wire, yDelay) return y
import lib601.sm as sm def neg(inp): return not(inp) negate = sm.PureFunction(neg) alternating = sm.Feedback(sm.Cascade(negate, sm.Delay(True))) print alternating.transduce([1,2,3,True,False])
import lib601.sm as sm # Define negate to be an instance of sm.PureFunction that takes a boolean # and returns the negation of it def negFunct(boolean): return not boolean class PureFunction(sm.SM): def __init__(self, f): self.f = f def getNextValues(self,state,inp): return (state,self.f(inp)) negate = sm.PureFunction(negFunct) # negate.transduce([True,False],verbose = True) # Use sm.Feedback,sm.Cascade, and negate to construct an instance whose output alternates # between True and False for any input sequence; starting with true. alternating = sm.Feedback(sm.Cascade(sm.Delay(False),negate)) alternating.transduce([1,True,False,True,False,True,False],verbose = True) # Not totally sure how I made this work, but hey it does.