1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
% The luanumint package
% version 1.2
% Licensed under LaTeX Project Public License v1.3c or later. The complete license text is available at http://www.latex-project.org/lppl.txt.
%Authors: Chetan Shirore and Ajit Kumar
\ProvidesPackage{luanumint}[1.2]
\RequirePackage{luacode,breqn,xkeyval}
\begin{luacode*}
function checksign(x)
if x >=0 then
return ' + '
else
return ''
end
end
-- Function to round off numbers.
function mathrnd(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
-- Function for the midpoint rule.
function luamidpt (f,a,b,n,trun)
local trun = trun or 4
local h = (b - a) / n
local sum = 0
local var = a + h/2
for i = 1, n do
sum = sum + f(var)
var = var + h
end
return mathrnd(sum*h,trun)
end
-- Function for the midpoint rule with steps.
function luamidptSteps (f,a,b,n,nm,trun)
local trun = trun or 4
local nm = nm or "f"
local h = (b - a) / n
local sum = 0
local var = a + (h/2)
local str = " = "..mathrnd(h,trun)..'\\left['
local otstr = mathrnd(h,trun)..'\\left('
for i = 1, n do
if i>=1 and i<=n-1 then
sum = sum + mathrnd(f(var),trun)
str = str ..nm.."("..mathrnd(var,trun)..") + "
otstr = otstr ..mathrnd(f(var),trun)..checksign(f(var+h),trun)
end
if i== n then
sum = sum + mathrnd(f(var),trun)
str = str ..nm.."("..var..")"
otstr = otstr ..mathrnd(f(var),trun)
end
var = var + h
end
return str .."\\right] \\\\ = "..otstr.."\\right) \\\\ = "..mathrnd(sum*mathrnd(h,trun),trun)
end
-- Function for the Trapezoidal rule.
function luatrapz (f,a,b,n,trun)
local trun = trun or 4
local h = (b - a) / n
local sum = 0
local var = a
for i = 1, n+1 do
if i== 1 or i == n+1 then
sum = sum + f(var)
else
sum = sum + 2*f(var)
end
var = var + h
end
return mathrnd(sum*h/2,trun)
end
-- Function for the Trapezoidal rule with Steps.
function luatrapzsteps (f,a,b,n,nm,trun)
local trun = trun or 4
local nm = nm or "f"
local h = (b - a) / n
local sum = 0
local var = a
local str = " = "..mathrnd(h/2,trun)..'\\left['
local otstr = mathrnd(h/2,trun)..'\\left('
for i = 0, n do
if i== 0 then
sum = sum + mathrnd(f(var),trun)
str = str ..nm.."("..var..") +"
otstr = otstr ..mathrnd(f(var),trun)..checksign(f(var+h),trun)
end
if i>=1 and i<=n-1 then
sum = sum + mathrnd(2*f(var),trun)
str = str .."2"..nm.."("..mathrnd(var,trun)..") + "
otstr = otstr ..mathrnd(2*f(var),trun)..checksign(f(var+h),trun)
end
if i== n then
sum = sum + mathrnd(f(var),trun)
str = str ..nm.."("..var..")"
otstr = otstr ..mathrnd(f(var),trun)
end
var = var + h
end
return str .."\\right] \\\\ = "..otstr.."\\right) \\\\ = "..mathrnd(sum*mathrnd(h/2,trun),trun)
end
-- Function for the Simpsons one-third rule.
function luasimpsononethird (f,a,b,n,trun)
if (not(n % 2 == 0)) then error("Number of subintervals should be even.") end
local trun = trun or 4
local h = (b - a) / n
local sum = 0
local var = a
for i = 0, n do
if i== 0 or i ==n then
sum = sum + f(var)
end
if i>=1 and i<=n-1 and (i % 2 == 0) then
sum = sum + 2*f(var)
end
if i>=1 and i<=n-1 and (not(i % 2 == 0)) then
sum = sum + 4*f(var)
end
var = var + h
end
return mathrnd(sum*h/3,trun)
end
-- Function for the Simpsons one-third rule with steps.
function luasimpsononethirdsteps (f,a,b,n,nm,trun)
if (not(n % 2 == 0)) then error("Number of subintervals should be even.") end
local trun = trun or 4
local nm = nm or "f"
local h = (b - a) / n
local sum = 0
local var = a
local str = " = "..mathrnd(h/3,trun)..'\\left['
local otstr = mathrnd(h/3,trun)..'\\left('
for i = 0, n do
if i== 0 then
sum = sum + mathrnd(f(var),trun)
str = str ..nm.."("..var..") +"
otstr = otstr ..mathrnd(f(var),trun)..checksign(f(var+h),trun)
end
if i>=1 and i<=n-1 and (i % 2 == 0) then
sum = sum + mathrnd(2*f(var),trun)
str = str .."2"..nm.."("..mathrnd(var,trun)..") + "
otstr = otstr ..mathrnd(2*f(var),trun)..checksign(f(var+h),trun)
end
if i>=1 and i<=n-1 and (not(i % 2 == 0)) then
sum = sum + mathrnd(4*f(var),trun)
str = str.."4"..nm.."("..mathrnd(var,trun)..") + "
otstr = otstr ..mathrnd(4*f(var),trun)..checksign(f(var+h),trun)
end
if i== n then
sum = sum + mathrnd(f(var),trun)
str = str ..nm.."("..var..")"
otstr = otstr ..mathrnd(f(var),trun)
end
var = var + h
end
return str .."\\right] \\\\ = "..otstr.."\\right) \\\\ = "..mathrnd(sum*mathrnd(h/3,trun),trun)
end
-- Function for the Simpsons three-eighth rule.
function luasimpsonthreight (f,a,b,n,trun)
if (not(n % 3 == 0)) then error("No. of sub-intervals should be multiple of 3.") end
local trun = trun or 4
local h = (b - a) / n
local sum = 0
local var = a
for i = 0, n do
if i== 0 or i ==n then
sum = sum + f(var)
end
if i>=1 and i<=n-1 and (i % 3 == 0) then
sum = sum + 2*f(var)
end
if i>=1 and i<=n-1 and (not(i % 3 == 0)) then
sum = sum + 3*f(var)
end
var = var + h
end
return mathrnd(sum*3*h/8,trun)
end
-- Function for the Simpsons three-eighth rule with steps.
function luasimpsonthreightsteps (f,a,b,n,nm,trun)
if (not(n % 3 == 0)) then error("No. of sub-intervals should be multip.") end
local trun = trun or 4
local nm = nm or "f"
local h = (b - a) / n
local sum = 0
local var = a
local str = " = "..mathrnd(3*h/8,trun)..'\\left['
local otstr = mathrnd(3*h/8,trun)..'\\left('
for i = 0, n do
if i== 0 then
sum = sum + mathrnd(f(var),trun)
str = str ..nm.."("..var..") +"
otstr = otstr ..mathrnd(f(var),trun)..checksign(f(var+h),trun)
end
if i>=1 and i<=n-1 and (i % 3 == 0) then
sum = sum + mathrnd(2*f(var),trun)
str = str .."2"..nm.."("..mathrnd(var,trun)..") + "
otstr = otstr ..mathrnd(2*f(var),trun)..checksign(f(var+h),trun)
end
if i>=1 and i<=n-1 and (not(i % 3 == 0)) then
sum = sum + mathrnd(3*f(var),trun)
str = str.."3"..nm.."("..mathrnd(var,trun)..") + "
otstr = otstr ..mathrnd(3*f(var),trun)..checksign(f(var+h),trun)
end
if i== n then
sum = sum + mathrnd(f(var),trun)
str = str ..nm.."("..var..")"
otstr = otstr ..mathrnd(f(var),trun)
end
var = var + h
end
return str .."\\right] \\\\ = "..otstr.."\\right) \\\\ = "..mathrnd(sum*mathrnd(3*h/8,trun),trun)
end
\end{luacode*}
% ========= KEY DEFINITIONS =========
\define@key{someop}{a}{\def\mop@onex{#1}}%
\define@key{someop}{b}{\def\mop@twox{#1}}%
\define@key{someop}{n}{\def\mop@threex{#1}}%
\define@key{someop}{func}{\def\mop@fourx{#1}}%
\define@key{someop}{trun}{\def\mop@fivex{#1}}%
% ========= KEY DEFAULTS =========
\setkeys{someop}{a=0}%
\setkeys{someop}{b=1}%
\setkeys{someop}{n=6}%
\setkeys{someop}{func=f}%
\setkeys{someop}{trun=4}%
% ========= Defining Command =========
\newcommand{\luaMidpt}[2][]{{%
\setkeys{someop}{#1}%
\directlua{%
tempsubexp = "("..\luastring{#2}..")"
local f = load("return function(x) return " ..tempsubexp.. "end",nil,"t",math)()
tex.print(luamidpt(f,\mop@onex,\mop@twox,\mop@threex,\mop@fivex))
}%
}%
}%
\newcommand{\luaMidptSteps}[2][]{\begingroup%
\setkeys{someop}{#1}%
\directlua{%
tempsubexp = "("..\luastring{#2}..")"
local f = load("return function(x) return " ..tempsubexp.. "end",nil,"t",math)() tex.sprint(luamidptSteps(f,\mop@onex,\mop@twox,\mop@threex,'\mop@fourx',\mop@fivex))
}%
\endgroup}%
\newcommand{\luaTrapz}[2][]{{%
\setkeys{someop}{#1}%
\directlua{%
tempsubexp = "("..\luastring{#2}..")"
local f = load("return function(x) return " ..tempsubexp.. "end",nil,"t",math)()
tex.print(luatrapz(f,\mop@onex,\mop@twox,\mop@threex,\mop@fivex))
}%
}%
}%
\newcommand{\luaTrapzSteps}[2][]{\begingroup%
\setkeys{someop}{#1}%
\directlua{%
tempsubexp = "("..\luastring{#2}..")"
local f = load("return function(x) return " ..tempsubexp.. "end",nil,"t",math)() tex.sprint(luatrapzsteps(f,\mop@onex,\mop@twox,\mop@threex,'\mop@fourx',\mop@fivex))
}%
\endgroup}%
\newcommand{\luaSimpsonOneThird}[2][]{{%
\setkeys{someop}{#1}%
\directlua{%
tempsubexp = "("..\luastring{#2}..")"
local f = load("return function(x) return " ..tempsubexp.. "end",nil,"t",math)() tex.print(luasimpsononethird(f,\mop@onex,\mop@twox,\mop@threex,\mop@fivex))
}%
}%
}%
\newcommand{\luaSimpsonOneThirdSteps}[2][]{\begingroup%
\setkeys{someop}{#1}%
\directlua{%
tempsubexp = "("..\luastring{#2}..")"
local f = load("return function(x) return " ..tempsubexp.. "end",nil,"t",math)() tex.sprint(luasimpsononethirdsteps(f,\mop@onex,\mop@twox,\mop@threex,'\mop@fourx',\mop@fivex))
}%
\endgroup}%
\newcommand{\luaSimpsonThreeEighth}[2][]{{%
\setkeys{someop}{#1}%
\directlua{%
tempsubexp = "("..\luastring{#2}..")"
local f = load("return function(x) return " ..tempsubexp.. "end",nil,"t",math)() tex.print(luasimpsonthreight(f,\mop@onex,\mop@twox,\mop@threex,\mop@fivex))
}%
}%
}%
\newcommand{\luaSimpsonThreeEighthSteps}[2][]{\begingroup%
\setkeys{someop}{#1}%
\directlua{%
tempsubexp = "("..\luastring{#2}..")"
local f = load("return function(x) return " ..tempsubexp.. "end",nil,"t",math)() tex.sprint(luasimpsonthreightsteps(f,\mop@onex,\mop@twox,\mop@threex,'\mop@fourx',\mop@fivex))
}%
\endgroup}%
\endinput
|