# How to Make Table with Computation Result in Julia (Using DataFrames Package or similar ones)

Hi all,

I have recently modify the code to calculate all methods to approximate definite integral, my question is how to create a table with the resulting computation for different partitions (4, 8, 16).

This is the code with the function:

``````f(x) = sin(x^2)
a, b = 0, 2

function riemann(f::Function, a::Real, b::Real, n::Int; method="right")
if method == "right"
meth = f -> (lr -> begin l,r = lr; f(r) * (r-l) end)
xs = range(a, b, n+1)
pairs = zip(xs[begin:end-1], xs[begin+1:end]) # (x₀,x₁), …, (xₙ₋₁,xₙ)
sum(meth(f), pairs)
elseif method == "left"
meth = f -> (lr -> begin l,r = lr; f(l) * (r-l) end)
xs = range(a, b, n+1)
pairs = zip(xs[begin:end-1], xs[begin+1:end]) # (x₀,x₁), …, (xₙ₋₁,xₙ)
sum(meth(f), pairs)
elseif method == "trapezoid"
meth = f -> (lr -> begin l,r = lr; (1/2) * (f(l) + f(r)) * (r-l) end)
xs = range(a, b, n+1)
pairs = zip(xs[begin:end-1], xs[begin+1:end]) # (x₀,x₁), …, (xₙ₋₁,xₙ)
sum(meth(f), pairs)
elseif method == "simpsons"
meth = f -> (lr -> begin l,r = lr; (1/6) * (f(l) + 4*(f((l+r)/2)) + f(r)) * (r-l) end)
xs = range(a, b, n+1)
pairs = zip(xs[begin:end-1], xs[begin+1:end]) # (x₀,x₁), …, (xₙ₋₁,xₙ)
sum(meth(f), pairs)
elseif method == "midpoint"
xs = a:(b-a)/n:b
deltas = diff(xs)      # forms x2-x1, x3-x2, ..., xn-xn-1
cs = [(xs[i] + xs[i+1])/2 for i in 1:length(deltas)]
sum(f(cs[i]) * deltas[i] for i in 1:length(deltas))
end
end
``````

to test the result you can type at REPL:

``````riemann(f, 0, 2, 8, method="left")
riemann(f, 0, 2, 8, method="right")
riemann(f, 0, 2, 8, method="midpoint")
riemann(f, 0, 2, 8, method="trapezoid")
riemann(f, 0, 2, 8, method="simpsons")
``````

then I have this code to create truth tables that might be able to help construct the table,

``````# ⊻ = xor (exclusive or)
# ⊼ = nand (not and)
# ⊽ = nor (not or)

using DataFrames, PrettyTables
df=DataFrame(p=repeat([true, false],inner=4), q=repeat([true, false],inner=2,outer=2), r=repeat([true, false],outer=4))

df[!,"~p"].=.!(df.p)
#df[!,"~q"].=.!(df.q)
#df[!,"~r"].=.!(df.r)
#df[!,"p ⩓ q"].=df.p.&&df.q
#df[!,"p ⩔ q"].=df.p.||df.q
#df[!,"p ⊻ q"] .=df.p .⊻ df.q
#df[!,"p ⊼ q"] .=df.p .⊼ df.q
#df[!,"p ⊽ q"] .=df.p .⊽ df.q
df[!,"~(p ⩓ ~p"].=.!(df.p.&&.!(df.p))
df[!,"p ⩔ ~p"].=df.p.||.!(df.p)
df[!,"p ⩔ q ⩔ r ⩔ ~(r ⩓ q)"].= (df.p.||df.q.||df.r) .|| .!(df.r .&& df.q)