up [pdf]
from rsf.proj import *

Flow('time',None,
     'math n1=200 d1=1 o1=1 output="(x1-1)*0.4" ')
Flow('head','time',"window n1=41 | math output='exp(0.25*input)+11'")
Flow('ideal','time',"math output='sin(input)*0.5'")
Flow('alias','head',"math output='sin(input)*0.5' | put head=$SOURCE")

doty = '''
dots dots=0 connect=0 gaineach=0 constsep=1 strings=0 clip=1.2 label1=Sample
'''

Flow('data','alias','bin1 nx=200 dx=0.4 x0=0')
Plot('data',doty + ' strings=1 d1=1 o1=1')
Plot('ideal',doty + ' connect=1')
Plot('both','data ideal','Overlay')
Result('data','both data','SideBySideAniso')

doty = doty + ' connect=1 d1=1 o1=1'

name = 'if'
title = ['Regularization','Preconditioning']
for prec in [0,1]:
    iters = []
    res = name[prec] + 'm'
    labels = ''
    for niter in [2,4,8,16,300]:
        iter = res + str(niter)
        iters.append(iter)
        Flow(iter,'alias',
             'invbin1 nx=200 dx=0.4 x0=0 filter=1 prec=%d niter=%d' % (prec,niter))
        labels = labels + 'iter=%d:' % niter
    Plot(res,iters,
         '''
         cat axis=2 ${SOURCES[1:%d]} | %s connect=1 overlap=2
         title=%s labels=%s labelsz=10 
         ''' % (len(iters),doty,title[prec],labels))
Result('conv',['im','fm'],'SideBySideAniso')

for prec in [0,1]:
    iters = []
    error = name[prec] + 'e'
    for niter in range(100):
        iter = error + str(niter)
        iters.append(iter)
        Flow(iter,['alias',name[prec]+'m300'],
             '''
             invbin1 nx=200 dx=0.4 x0=0
             filter=1 prec=%d niter=%d |
             math refer=${SOURCES[1]} output="(input-refer)^2" |
             stack axis=1 norm=no | math output="sqrt(input)"
             ''' % (prec,niter+1))
    Flow(error,iters,'cat axis=1 ${SOURCES[1:%d]}' % len(iters))

Result('schwab1','ie fe',
       '''
       cat axis=2 ${SOURCES[1]} | put d1=1 o1=0 |
       graph label1="Iterations" label2="Error"
       symbol=rp symbolsz=7,7 
       title="Model Residual Vector Length"
       min2=0 min1=-1
       ''')

###########################################################################

End()

sfmath
sfwindow
sfput
sfbin1
sfdots
sfinvbin1
sfcat
sfstack
sfgraph