From d4f9c76490dc8b52de2a631eb18eab0f80bc6d66 Mon Sep 17 00:00:00 2001 From: Sathvik Bhagavan Date: Fri, 20 Oct 2023 10:02:48 +0530 Subject: [PATCH] feat: add show methods --- ext/DataInterpolationsOptimExt.jl | 9 ++- ...ataInterpolationsRegularizationToolsExt.jl | 6 +- src/DataInterpolations.jl | 2 + src/show.jl | 65 +++++++++++++++++++ 4 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 src/show.jl diff --git a/ext/DataInterpolationsOptimExt.jl b/ext/DataInterpolationsOptimExt.jl index 93f32d70..3d080b83 100644 --- a/ext/DataInterpolationsOptimExt.jl +++ b/ext/DataInterpolationsOptimExt.jl @@ -2,11 +2,11 @@ module DataInterpolationsOptimExt if isdefined(Base, :get_extension) using DataInterpolations: AbstractInterpolation, munge_data - import DataInterpolations: Curvefit, _interpolate + import DataInterpolations: Curvefit, _interpolate, get_show using Reexport else using ..DataInterpolations: AbstractInterpolation, munge_data - import ..DataInterpolations: Curvefit, _interpolate + import ..DataInterpolations: Curvefit, _interpolate, get_show using Reexport end @@ -75,4 +75,9 @@ function _interpolate(A::CurvefitCache{<:AbstractVector{<:Number}}, _interpolate(A, t), i end +function get_show(interp::CurvefitCache) + return "Curvefit" * " with $(length(interp.t)) points, using $(nameof(typeof(interp.alg)))\n" +end + + end # module diff --git a/ext/DataInterpolationsRegularizationToolsExt.jl b/ext/DataInterpolationsRegularizationToolsExt.jl index fe5e7d75..1d379e53 100644 --- a/ext/DataInterpolationsRegularizationToolsExt.jl +++ b/ext/DataInterpolationsRegularizationToolsExt.jl @@ -1,7 +1,7 @@ module DataInterpolationsRegularizationToolsExt using DataInterpolations -using DataInterpolations: munge_data, _interpolate, RegularizationSmooth +using DataInterpolations: munge_data, _interpolate, RegularizationSmooth, get_show using LinearAlgebra isdefined(Base, :get_extension) ? (import RegularizationTools as RT) : @@ -269,4 +269,8 @@ function DataInterpolations._interpolate(A::RegularizationSmooth{ DataInterpolations._interpolate(A.Aitp, t) end +function DataInterpolations.get_show(interp::RegularizationSmooth) + return "RegularizationSmooth" * " with $(length(interp.t)) points, with regularization coefficient $(interp.λ)\n" +end + end # module diff --git a/src/DataInterpolations.jl b/src/DataInterpolations.jl index b5f36a35..96f9ebab 100644 --- a/src/DataInterpolations.jl +++ b/src/DataInterpolations.jl @@ -16,6 +16,7 @@ function Base.setindex!(A::AbstractInterpolation{true}, x, i) end using LinearAlgebra, RecursiveArrayTools, RecipesBase +using PrettyTables include("interpolation_caches.jl") include("interpolation_utils.jl") @@ -24,6 +25,7 @@ include("plot_rec.jl") include("derivatives.jl") include("integrals.jl") include("online.jl") +include("show.jl") (interp::AbstractInterpolation)(t::Number) = _interpolate(interp, t) (interp::AbstractInterpolation)(t::Number, i::Integer) = _interpolate(interp, t, i) diff --git a/src/show.jl b/src/show.jl new file mode 100644 index 00000000..e00dcaae --- /dev/null +++ b/src/show.jl @@ -0,0 +1,65 @@ +###################### Generic Dispatches ###################### + +function Base.show(io::IO, mime::MIME"text/plain", interp::AbstractInterpolation) + print(io, get_show(interp)) + header = ["time", get_names(interp.u)...] + data = hcat(interp.t, get_data(interp.u)) + pretty_table(io, data; header = header, vcrop_mode = :middle) +end + +function get_show(interp::AbstractInterpolation) + return string(nameof(typeof(interp))) * " with $(length(interp.t)) points\n" +end + +function get_data(u::AbstractVector) + return u +end + +function get_data(u::AbstractVector{<:AbstractVector}) + return reduce(hcat, u)' +end + +function get_data(u::AbstractMatrix) + return u' +end + +function get_names(u::AbstractVector) + return ["u"] +end + +function get_names(u::AbstractVector{<:AbstractVector}) + return ["u$i" for i in eachindex(first(u))] +end + +function get_names(u::AbstractMatrix) + return ["u$i" for i in axes(u, 1)] +end + +###################### Specific Dispatches ###################### + +function get_show(interp::QuadraticInterpolation) + return string(nameof(typeof(interp))) * " with $(length(interp.t)) points, $(interp.mode) mode\n" +end + +function get_show(interp::LagrangeInterpolation) + return string(nameof(typeof(interp))) * " with $(length(interp.t)) points, with order $(interp.n)\n" +end + +function get_show(interp::ConstantInterpolation) + return string(nameof(typeof(interp))) * " with $(length(interp.t)) points, in $(interp.dir) direction\n" +end + +function get_show(interp::BSplineInterpolation) + return string(nameof(typeof(interp))) * " with $(length(interp.t)) points, with degree $(interp.d)\n" +end + +function get_show(interp::BSplineApprox) + return string(nameof(typeof(interp))) * " with $(length(interp.t)) points, with degree $(interp.d), number of control points $(interp.h)\n" +end + + + + + + +