itextsharp pdf to image c# : Add text field pdf SDK Library project winforms .net web page UWP book16-part1761

3.5 Exercises
131
��
��
��
��
��
��
��
��
Implement(3.10)inaPythonfunctiontrapezint(f, a, b, n).Run
theexamplesfromb)withn=10.
e) Writeatestfunctiontest_trapezint()forverifyingtheimplemen-
tationofthefunctiontrapezintind).
Hint. Obviously, the Trapezoidalmethodintegrateslinearfunctions
exactlyforanyn.Anothermoresurprisingresultisthatthemethodis
alsoexactfor,e.g.,
0
cosxdxforanyn.
Filename:trapezint.py.
Remarks. Formula(3.10)isnotthemostcommonwayofexpressing
theTrapezoidalintegrationrule.Thereasonisthatf(x
i+1
)isevaluated
twice,firstintermiandthenasf(x
i
)intermi+1.Theformulacan
befurtherdevelopedtoavoidunnecessaryevaluationsoff(x
i+1
),which
resultsinthestandardform
b
a
f(x)dx≈
1
2
h(f(a)+f(b))+h
n−1
i=1
f(x
i
).
(3.11)
Exercise3.7:DerivethegeneralMidpointintegrationrule
TheideaoftheMidpointruleforintegrationistodividetheareaunder
thecurvef(x)intonequal-sizedrectangles(insteadoftrapezoidsasin
Exercise3.6).Theheightoftherectangleisdeterminedbythevalueof
fatthemidpointoftherectangle.Thefigurebelowillustratestheidea.
Add text field pdf - C# PDF Field Edit Library: insert, delete, update pdf form field in C#.net, ASP.NET, MVC, Ajax, WPF
Online C# Tutorial to Insert, Delete and Update Fields in PDF Document
chrome save pdf with fields; add forms to pdf
Add text field pdf - VB.NET PDF Field Edit library: insert, delete, update pdf form field in vb.net, ASP.NET, MVC, Ajax, WPF
How to Insert, Delete and Update Fields in PDF Document with VB.NET Demo Code
change font size in pdf fillable form; cannot save pdf form in reader
132
3 Functionsandbranching
Computetheareaofeachrectangle,sumthemup,andarriveatthe
formulafortheMidpointrule:
b
a
f(x)dx≈h
n−1
i=0
f(a+ih+
1
2
h),
(3.12)
where= (b−a)/n isthe widthofeach rectangle.Implement this
formulainaPythonfunctionmidpointint(f, a, b, n)andtestthe
functionontheexampleslistedinExercise3.6b.Howdotheerrorsin
theMidpointrulecomparewiththoseoftheTrapezoidalruleforn=1
andn=10?Filename:midpointint.py.
Exercise3.8:MakeanadaptiveTrapezoidalrule
AproblemwiththeTrapezoidalintegrationrule(3.10)inExercise3.6is
todecidehowmanytrapezoids(n)touseinordertoachieveadesired
accuracy.LetEbetheerrorintheTrapezoidalmethod,i.e.,thedifference
betweentheexactintegralandthatproducedby(3.10).Wewouldlike
toprescribea(small)tolerance�andfindannsuchthatE≤�.
Sincetheexactvalue
b
a
f(x)dxisnotavailable(thatiswhyweusea
numericalmethod!),itischallengingtocomputeE.Nevertheless,ithas
beenshownbymathematiciansthat
E≤
1
12
(b−a)h
2
max
x∈[a,b]
|f
��
(x)|.
(3.13)
Themaximumof|f��(x)|canbecomputed(approximately)byevaluating
f��(x) at alarge number ofpointsin[a,b],taking the absolutevalue
C# PDF insert image Library: insert images into PDF in C#.net, ASP
Insert images into PDF form field. Access to freeware download and online C#.NET class source code. How to insert and add image, picture, digital photo, scanned
add print button to pdf form; changing font size in pdf form
VB.NET PDF insert image library: insert images into PDF in vb.net
Insert images into PDF form field in VB.NET. with this sample VB.NET code to add an image PDFDocument = New PDFDocument(inputFilePath) ' Get a text manager from
add text field to pdf; create a fillable pdf form from a word document
3.5 Exercises
133
|f��(x)|,andfindingthemaximumvalueofthese.Thedoublederivative
canbecomputedbyafinitedifferenceformula:
f
��
(x)≈
f(x+h)−2f(x)+f(x−h)
h2
.
Withthecomputedestimateofmax|f��(x)|wecanfindhfromsetting
theright-handsidein(3.13)equaltothedesiredtolerance:
1
12
(b−a)h
2
max
x∈[a,b]
|f
��
(x)|=�.
Solvingwithrespecttohgives
h=
12�
(b−a)max
x∈[a,b]
|f
��
(x)|
−1/2
.
(3.14)
With= (b−a)/h we have the that corresponds s to o the desired
accuracy�.
a) MakeaPythonfunctionadaptive_trapezint(f, , a, b, eps=1E-5)
forcomputingtheintegral
b
a
f(x)dxwithanerrorlessthanorequalto
�(eps).
Hint. Computethencorrespondingtoasexplainedaboveandcall
trapezint(f, a, b, n)fromExercise3.6.
b) ApplythefunctiontocomputetheintegralsfromExercise3.6b.Write
outtheexacterrorandtheestimatednforeachcase.
Filename:adaptive_trapezint.py.
Remarks. Anumericalmethodthatappliesanexpressionfortheerror
toadaptthechoiceofthediscretizationparametertoadesirederror
tolerance,isknownasanadaptivenumericalmethod.Theadvantage
ofanadaptivemethodisthatonecancontroltheapproximationerror,
andthereisnoneedfortheusertodetermineanappropriatenumberof
intervalsn.
Exercise3.9:Explainwhyaprogramworks
Explainhowandtherebywhythefollowingprogramworks:
def add(A, B):
C = = A + + B
return C
A = = 3
B = = 2
print add(A, B)
VB.NET PDF Text Extract Library: extract text content from PDF
With this advanced PDF Add-On, developers are able to extract target text content from source PDF document and save extracted text to other file formats
cannot save pdf form in reader; adding an image to a pdf form
C# PDF Text Extract Library: extract text content from PDF file in
How to C#: Extract Text Content from PDF File. Add necessary references: RasterEdge.Imaging.Basic.dll. RasterEdge.Imaging.Basic.Codec.dll.
add jpg to pdf form; pdf add signature field
134
3 Functionsandbranching
Exercise3.10:Simulateaprogrambyhand
Simulatethefollowingprogrambyhandtoexplainwhatisprinted.
def a(x):
q = = 2
x = = 3*x
return q q + x
def b(x):
global q
q += = x
return q q + x
q = 0
x = 3
print a(x), , b(x), , a(x), b(x)
Hint. Ifyouencounterproblemswithunderstandingfunctioncallsand
localversusglobalvariables,paste the code intothe OnlinePython
Tutorandstepthroughthecodetogetagoodexplanationofwhat
happens.
Exercise3.11:Computetheareaofanarbitrarytriangle
Anarbitrarytrianglecanbedescribedbythecoordinatesofitsthreever-
tices:(x
1
,y
1
),(x
2
,y
2
),(x
3
,y
3
),numberedinacounterclockwisedirection.
Theareaofthetriangleisgivenbytheformula
A=
1
2
|x
2
y
3
−x
3
y
2
−x
1
y
3
+x
3
y
1
+x
1
y
2
−x
2
y
1
|.
(3.15)
Writeafunctionarea(vertices) that returnstheareaofa triangle
whoseverticesarespecifiedbytheargumentvertices,whichisanested
listofthevertexcoordinates.Forexample,computingtheareaofthe
trianglewithvertexcoordinates(0,0),(1,0),and(0,2)isdoneby
triangle1 = = area([[0,0], , [1,0], [0,2]])
# or
v1 = = (0,0); v2 2 = = (1,0); v3 3 = = (0,2)
vertices = = [v1, , v2, v3]
triangle1 = = area(vertices)
print ’Area of f triangle e is %.2f’ ’ % % triangle1
Test the area function on n a a triangle with known area. . Filename:
area_triangle.py.
Exercise3.12:Computethelengthofapath
Someobject ismovingalongapathintheplane.Atn+1pointsof
timewehaverecordedthecorresponding(x,y)positionsoftheobject:
8
http://www.pythontutor.com/visualize.html
VB.NET PDF Password Library: add, remove, edit PDF file password
VB: Add Password to PDF with Permission Settings Applied. This VB.NET example shows how to add PDF file password with access permission setting.
create a fillable pdf form; add image to pdf form
C# PDF Password Library: add, remove, edit PDF file password in C#
C# Sample Code: Add Password to PDF with Permission Settings Applied in C#.NET. This example shows how to add PDF file password with access permission setting.
add submit button to pdf form; change font size pdf fillable form
3.5 Exercises
135
(x
0
,y
0
),(x
1
,y
2
),...,(x
n
,y
n
).ThetotallengthLofthepathfrom(x
0
,y
0
)
to(x
n
,y
n
)isthesumofalltheindividuallinesegments((x
i−1
,y
i−1
)to
(x
i
,y
i
),i=1,...,n):
L=
n
i=1
(x
i
−x
i−1
)2+(y
i
−y
i−1
)2.
(3.16)
a) MakeaPythonfunctionpathlength(x, y)forcomputingLaccord-
ingtotheformula.Theargumentsxandyholdallthex
0
,...,x
n
and
y
0
,...,y
n
coordinates,respectively.
b) Write a a test function test_pathlength() where you u checkthat
pathlengthreturnsthecorrectlengthinatestproblem.
Filename:pathlength.py.
Exercise3.13:Approximateπ
The value of π equals s the e circumference of acircle with radius s 1/2.
Supposeweapproximatethecircumferencebyapolygonthroughn+1
pointsonthecircle.Thelengthofthispolygoncanbefoundusingthe
pathlengthfunctionfromExercise3.12.Computen+1points(x
i
,y
i
)
alongacirclewithradius1/2accordingtotheformulas
x
i
=
1
2
cos(2πi/n), y
i
=
1
2
sin(2πi/n), i=0,...,n.
Callthepathlengthfunctionandwriteouttheerrorintheapproxima-
tionofπforn=2k,k=2,3,...,10.Filename:pi_approx.py.
Exercise3.14:Writefunctions
Threefunctions,hw1,hw2,andhw3,workasfollows:
>>> print t hw1()
Hello, World!
>>> hw2()
Hello, World!
>>> print t hw3(’Hello, , ’, , ’World!’)
Hello, World!
>>> print t hw3(’Python n ’, , ’function’)
Python function
Writethethreefunctions.Filename:hw_func.py.
Exercise3.15:Approximateafunctionbyasumofsines
Weconsiderthepiecewiseconstantfunction
VB.NET PDF Text Add Library: add, delete, edit PDF text in vb.net
Data: Auto Fill-in Field Data. Field: Insert, Delete, Update Field. Redact Text Content. Redact Images. Redact Pages. Annotation & Drawing. Add Sticky Note.
add print button to pdf form; add attachment to pdf form
C# PDF Text Add Library: add, delete, edit PDF text in C#.net, ASP
Data: Auto Fill-in Field Data. Field: Insert, Delete, Update Field. Redact Text Content. Redact Images. Redact Pages. Annotation & Drawing. Add Sticky Note.
chrome save pdf form; acrobat create pdf form
136
3 Functionsandbranching
f(t)=
1, 0<t<T/2,
0, t=T/2,
−1,T/2<t<T
(3.17)
Sketchthisfunctiononapieceofpaper.Onecanapproximatef(t)by
thesum
S(t;n)=
4
π
n
i=1
1
2i−1
sin
2(2i−1)πt
T
.
(3.18)
ItcanbeshownthatS(t;n)→f(t)asn→∞.
a)WriteaPythonfunctionS(t, n, T)forreturningthevalueofS(t;n).
b) WriteaPythonfunctionf(t, , T)forcomputingf(t).
c) Writeouttabularinformationshowinghowtheerrorf(t)−S(t;n)
varieswithnandtforthecaseswheren=1,3,5,10,30,100andt=αT,
withT=2π,andα=0.01,0.25,0.49.Usethetabletocommentonhow
thequalityoftheapproximationdependsonαandn.
Filename:sinesum1.py.
Remarks. Asumofsineand/orcosinefunctions,asin(3.18),iscalled
aFourierseries.ApproximatingafunctionbyaFourierseriesisavery
importanttechniqueinscienceandtechnology.Exercise5.39asksfor
visualizationofhowwellS(t;n)approximatesf(t)forsomevaluesofn.
Exercise3.16:ImplementaGaussianfunction
MakeaPythonfunctiongauss(x, m=0, s=1)forcomputingtheGaus-
sianfunction
f(x)=
1
2πs
exp
1
2
x−m
s
2
.
Writeoutanicelyformattedtableofxandf(x)valuesfornuniformly
spacedxvaluesin[m−5s,m+5s].(Choosem,s,andnasyoulike.)
Filename:gaussian2.py.
Exercise3.17:Wrapaformulainafunction
Implementtheformula(1.9)fromExercise1.12inaPythonfunction
withthreearguments:egg(M, To=20, Ty=70).Theparametersρ,K,c,
andT
w
canbesetaslocal(constant)variablesinsidethefunction.Lett
bereturnedfromthefunction.Computetforasoftandhardboiledegg,
ofasmall(M=47g)andlarge(M=67g)size,takenfromthefridge
(T
o
=4C)andfromahotroom(T
o
=25C).Filename:egg_func.py.
3.5 Exercises
137
Exercise3.18:Writeafunctionfornumericaldifferentiation
Theformula
f
(x)≈
f(x+h)−f(x−h)
2h
(3.19)
canbeusedtofindanapproximatederivativeofamathematicalfunction
f(x)ifhissmall.
a) Writeafunctiondiff(f, x, h=1E-5)thatreturnstheapproxima-
tion(3.19)ofthederivativeofamathematicalfunctionrepresentedbya
Pythonfunctionf(x).
b) Writeafunctiontest_diff()thatverifiestheimplementationof
thefunctiondiff.Astestcase,onecanusethefactthat(3.19)isexact
forquadraticfunctions.Followtheconventionsofthepytestandnose
testingframeworks,asoutlinedinExercise3.2andSections3.3.3,3.4.2,
andH.6.
c) Apply(3.19)todifferentiate
•f(x)=eatx=0,
•f(x)=e
−2x2
atx=0,
•f(x)=cosxatx=2π,
•f(x)=lnxatx=1.
Useh=0.01.Ineachcase,writeouttheerror,i.e.,thedifferencebetween
theexactderivativeandtheresultof(3.19).Collectthesefourexamples
inafunctionapplication().
Filename:centered_diff.py.
Exercise3.19:Implementthefactorialfunction
Thefactorialofniswrittenasn!anddefinedas
n!=n(n−1)(n−2)···2·1,
(3.20)
withthespecialcases
1!=1, 0!=1.
(3.21)
Forexample,4!=4·3·2·1=24,and2!=2·1=2.WriteaPythonfunction
fact(n)thatreturnsn!.(Donotsimplycalltheready-madefunction
math.factorial(n)-thatisconsideredcheatinginthiscontext!)
Hint. Return1immediatelyifxis1or0,otherwiseusealooptocompute
n!.
Filename:fact.py.
138
3 Functionsandbranching
Exercise3.20:Computevelocityandaccelerationfrom1D
positiondata
SupposewehaverecordedGPScoordinatesx
0
,...,x
n
attimest
0
,...,t
n
whilerunningordrivingalongastraightroad.Wewanttocomputethe
velocityv
i
andaccelerationa
i
fromthesepositioncoordinates.Using
finitedifferenceapproximations,onecanestablishtheformulas
v
i
x
i+1
−x
i−1
t
i+1
−t
i−1
)
,
(3.22)
a
i
≈2(t
i+1
−t
i−1
)
−1
x
i+1
−x
i
t
i+1
−t
i
x
i
−x
i−1
t
i
−t
i−1
,
(3.23)
fori=1,...,n−1.
a)WriteaPythonfunctionkinematics(x, i, dt=1E-6)forcomputing
v
i
anda
i
,giventhearrayxofpositioncoordinatesx
0
,...,x
n
.
b) Write a Python function test_kinematics() for testing the im-
plementationinthecaseofconstantvelocityV.Sett
0
=0,t
1
=0.5,
t
2
=1.5,andt
3
=2.2,andx
i
=Vt
i
.
Filename:kinematics1.py.
Exercise3.21:Findthemaxandminvaluesofafunction
Themaximumandminimumvaluesofamathematicalfunctionf(x)on
[a,b]canbefoundbycomputingfatalargenumber(n)ofpointsand
selectingthemaximumandminimumvaluesat thesepoints.Writea
Pythonfunctionmaxmin(f, a, b, n=1000)thatreturnsthemaximum
andminimumvalueofafunctionf(x).Alsowriteatestfunctionfor
verifyingtheimplementationforf(x)=cosx,x∈[−π/2,2π].
Hint. Thexpointswherethemathematicalfunctionistobeevaluated
canbeuniformlydistributed:x
i
=a+ih,i=0,...,n−1,h=(b−a)/(n−
1).ThePythonfunctionsmax(y)andmin(y)returnthemaximumand
minimumvaluesinthelisty,respectively.
Filename:maxmin_f.py.
Exercise3.22:Findthemaxandminelementsinalist
Givenalista,themaxfunctioninPython’sstandardlibrarycomputes
thelargestelementina:max(a).Similarly,min(a)returnsthesmallest
elementina.Writeyourownmaxandminfunctions.
3.5 Exercises
139
Hint. Initialize a variable max_elem bythe first element t inthe e list,
thenvisitallthe remainingelements(a[1:]),compareeachelement
tomax_elem,andifgreater,setmax_elemequaltothatelement.Usea
similartechniquetocomputetheminimumelement.
Filename:maxmin_list.py.
Exercise3.23:ImplementtheHeavisidefunction
ThefollowingstepfunctionisknownastheHeavisidefunctionandis
widelyusedinmathematics:
H(x)=
0,x<0
1,x≥0
(3.24)
a) ImplementH(x)inaPythonfunctionH(x).
b) MakeaPythonfunctiontest_H()fortestingtheimplementationof
H(x).ComputeH(10),H(10
−15
),H(0),H(10
−15
),H(10)andtest
thattheanswersarecorrect.
Filename:Heaviside.py.
Exercise3.24:ImplementasmoothedHeavisidefunction
TheHeavisidefunction(3.24)listedinExercise3.23isdiscontinuous.It
isinmanynumericalapplicationsadvantageoustoworkwithasmooth
versionoftheHeavisidefunctionwherethefunctionitselfanditsfirst
derivativearecontinuous.OnesuchsmoothedHeavisidefunctionis
H
(x)=
0,
x<−�,
1
2
+
x
2�
+
1
sin
πx
,−�≤x≤�
1,
x>�
(3.25)
a) ImplementH
(x)inaPythonfunctionH_eps(x, eps=0.01).
b)MakeaPythonfunctiontest_H_eps()fortestingtheimplementation
ofH_eps.Checkthevaluesofsomex<−�,x=−�,x=0,x=,and
somex>�.
Filename:smoothed_Heaviside.py.
Exercise3.25:Implementanindicatorfunction
Inmanyapplicationsthereisneedforanindicatorfunction,whichis1
oversomeintervaland0elsewhere.Moreprecisely,wedefine
I(x;L,R)=
1,x∈[L,R],
0,elsewhere
(3.26)
140
3 Functionsandbranching
a) Make twoPythonimplementationsof suchanindicator function,
onewithadirecttestifx∈[L,R]andonethatexpressestheindicator
functionintermsofHeavisidefunctions(3.24):
I(x;L,R)=H(x−L)H(R−x).
(3.27)
b)Makeatestfunctionforverifyingtheimplementationofthefunctions
ina).Checkthatcorrectvaluesarereturnedforsomex<L,x=L,
x=(L+R)/2,x=R,andsomex>R.
Filename:indicator_func.py.
Exercise3.26:Implementapiecewiseconstantfunction
Piecewiseconstantfunctionshavealotofimportantapplicationswhen
modelingphysicalphenomenabymathematics.Apiecewise constant
functioncanbedefinedas
f(x)=
v
0
,x∈[x
0
,x
1
),
v
1
,x∈[x
1
,x
2
),
.
.
.
v
i
x∈[x
i
,x
i+1
),
.
.
.
v
n
x∈[x
n
,x
n+1
]
(3.28)
Thatis,wehaveaunionofnon-overlappingintervalscoveringthedomain
[x
0
,x
n+1
],andf(x) isconstant ineachinterval.One example isthe
functionthatis-1on[0,1],0 on[1,1.5],and4on[1.5,2],where we
withthenotationin(3.28)havex
0
=0,x
1
=1,x
2
=1.5,x
3
=2and
v
0
=−1,v
1
=0,v
3
=4.
a) MakeaPythonfunctionpiecewise(x, , data)forevaluatingapiece-
wiseconstantmathematicalfunctionasin(3.28)atthepointx.The
dataobjectisalistofpairs(v
i
,x
i
)fori=0,...,n.Forexample,datais
[(0, -1), , (1, , 0), (1.5, 4)]intheexamplelistedabove.Sincex
n+1
isnotapartofthedataobject,wehavenomeansfordetectingwhether
xistotherightofthelastinterval[x
n
,x
n+1
],i.e.,wemustassumethat
theuserofthepiecewisefunctionsendsinanx≤x
n+1
.
b) Designsuitabletestcasesforthefunctionpiecewiseandimplement
theminatestfunctiontest_piecewise().
Filename:piecewise_constant1.py.
Exercise3.27:Applyindicatorfunctions
Implementpiecewiseconstantfunctions,asdefinedinExercise3.26,by
observingthat
Documents you may be interested
Documents you may be interested