"""Original 1997 LSTM cell arithmetic, with externally supplied gates.
No forget gate, training, learned recurrent gate projections, or peepholes.
The g and h functions follow the original appendix's scaled logistic choices.
"""
import argparse
import json
import math

# region: update
def g(x):
    return 2*math.tanh(x/2)  # equivalent to 4*sigmoid(x)-2


def cell_step(state, value, input_gate, output_gate):
    """The unit-weight self-loop adds gated writes; it never multiplies by f."""
    state = state + input_gate*g(value)
    return {'state': state, 'output': output_gate*h(state)}
# endregion: update

# region: readout
def h(state):
    return math.tanh(state/2)  # equivalent to 2*sigmoid(state)-1


def trajectory(inputs, input_gates, output_gates, initial=0.0):
    state, results = initial, []
    for value, write, read in zip(inputs,input_gates,output_gates):
        result=cell_step(state,value,write,read)
        state=result['state']; results.append(result)
    return results
# endregion: readout

# region: gradients
def gradient_path(steps, factor=0.8):
    """Compare isolated direct state paths, holding gates/writes fixed."""
    return {'cec': 1.0, 'recurrent': factor**steps}


def read_derivative(state, output_gate):
    """CEC derivative is one; the readout derivative can still saturate."""
    return output_gate*0.5*(1-h(state)**2)
# endregion: gradients


def main():
    inputs=[1,0,-1,0,0,0]; writes=[1,0,0,0,0,0]; reads=[0,0,0,0,0,1]
    values=trajectory(inputs,writes,reads)
    assert all(math.isclose(v['state'],g(1)) for v in values)
    eps=1e-5
    finite=(h(1+eps)-h(1-eps))/(2*eps)
    assert math.isclose(finite,read_derivative(1,1),rel_tol=1e-8)
    report={'trajectory':values,'gradient':gradient_path(20),'readDerivative':read_derivative(1,1)}
    parser=argparse.ArgumentParser();parser.add_argument('--json',action='store_true');args=parser.parse_args()
    print(json.dumps(report) if args.json else 'Original LSTM self-checks passed. '+str(report))

if __name__ == '__main__':
    main()
