这件事我该怎么办?我想在 x 时间内将一个值从一个值补间为另一个值。同时还要考虑到在开始和结束时有一个“轻松”的感觉会很好。
我知道,我真的不应该问,但我自己试过了,但我被困住了。请假设要引起延迟,您需要调用函数 wait(time)。
我正在尝试使用 Vue.js 构建一个评论页面,它将采用一个对象数组,并在页面上为数组中的每个评论插入一个部分。它还应该显示相应的名称、评级、评论文本等。
以下代码已完成一半工作。数据已正确设置到 Vue,并且正在构建页面上的所有 div,但是插值不起作用。div 为空;数据未显示。
超文本标记语言
<div class="reviews-holder" id="review-holder">
<div v-for="review of reviews" class="review-container">
<div class="row border-bottom">
<div class="col-sm-6 col-xs-12">
<h5>{{ review.name }}</h5>
<p>Reviewed on {{ review.time }}</p>
</div>
<div class="col-sm-6 col-xs-12">
<div class="pull-right rating rating-header">
{{ review.rating }}
</div>
</div>
</div>
<h4>{{ review.title }}</h4>
<span class="review-text">{{ review.review }}</span>
</div>
Run Code Online (Sandbox Code Playgroud)
JS
$(document).ready(function() {
$.post("/api/getReviews", dto, function(res){
if (res.ok) {
console.log("res.res", res.res);
var reviewsVue = new Vue({
el: '#review-holder',
data: {
reviews: res.res
},
components: {
VPaginator: VuePaginator
}, …Run Code Online (Sandbox Code Playgroud) 我正在使用 Python 3 执行与数值分析相关的任务。
我必须绘制一些源自正弦函数的点。此外,我需要对这些点进行三次插值(三次样条)。
这样,这些任务就完成了。输出图片很棒,代码有效。但是,我需要检查三次样条的导数是否看起来像一个余弦函数。
看看这张图片:
在橙色中,您会看到余弦函数。在蓝色中,您会看到正弦函数。红色的是我采样的 5 个点。在紫色中,您可以看到线性插值。而且,在破折号中,您会看到三次插值。
我需要绘制虚线曲线的导数并将其与橙色曲线进行比较。
直觉上,我知道它们会非常相似。但是,我无法用图表证明这一点。
那是代码:
import math
import random
from numpy import array
import numpy as np
import matplotlib.pyplot as plot
from scipy.interpolate import interp1d
from scipy import interpolate
from scipy.misc import derivative as deriv
def random_sine():
lista_f_x = []
lista_x = []
for i in range(1,6):
aleatorio = random.uniform(0,360)
aleatorio = math.radians(aleatorio)
lista_x.append(aleatorio)
sine_random = math.sin(aleatorio)
lista_f_x.append(sine_random)
lista_x = array(lista_x)
lista_f_x = array(lista_f_x)
return ("x",lista_x,"f(x)", lista_f_x)
# …Run Code Online (Sandbox Code Playgroud) interpolation matplotlib numerical-methods derivative python-3.x
背景
我试图复制《统计学习简介》一书中的图 2.6:
粗略的薄板样条拟合图 2.3 中的收入数据。这种拟合使训练数据的错误为零。
到目前为止我尝试过什么?
我尝试复制之前的图 2.5,平滑的薄板样条拟合,不确定是否成功。
income_2 <- read.csv("http://www-bcf.usc.edu/~gareth/ISL/Income2.csv")
library(mgcv)
model1 <- gam(Income ~ te(Education, Seniority, bs=c("tp", "tp")), data = income_2)
x <- range(income_2$Education)
x <- seq(x[1], x[2], length.out=30)
y <- range(income_2$Seniority)
y <- seq(y[1], y[2], length.out=30)
z <- outer(x,y,
function(Education,Seniority)
predict(model1, data.frame(Education,Seniority)))
p <- persp(x,y,z, theta=30, phi=30,
col="yellow",expand = 0.5,shade = 0.2,
xlab="Education", ylab="Seniority", zlab="Income")
obs <- trans3d(income_2$Education, income_2$Seniority,income_2$Income,p)
pred <- trans3d(income_2$Education, income_2$Seniority,fitted(model1),p)
points(obs, col="red",pch=16)
segments(obs$x, obs$y, pred$x, pred$y)
Run Code Online (Sandbox Code Playgroud)
双重问题
Given a two vectors x and y I can use scipy.interpolate.interp1d to compute the (quadratic) spline. However I'm not directly interested in the spline itself, but rather in the derivative of the spline.
I would prefer having an explicit solution rather than a numerical derivative.
但是我找不到多项式参数存储在哪里interp1d。我尝试过interp1d.__dict__,其中包含interp1d._spline但我没有找到该参数的定义是什么。
我有两条 svg 路径曲线想要设置动画。我希望在 5 秒内dPathBefore变得dPathNow平滑的动画过渡。
这是我的代码:
const dPathBefore = 'M 50 350 Q 150 0 200 250 Q 250 350 300 250 C 350 200 450 250 400 350 Z'
const dPathNow = 'M 50 350 C 50 150 150 150 250 300 C 300 150 400 150 400 350 Z'
export class PathAnimation extends React.Component {
render() {
return (
<svg width={500} height={400} className={'ba b--black'}>
<path d={dPathBefore} fill={'cyan'} fillOpacity={0.5} />
<path d={dPathNow} fill={'red'} fillOpacity={0.5} /> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Julia 中的 Interpolations.jl 和 Dierckx.jl 包进行函数插值。Interpolations.jl 的文档如下:
https://github.com/JuliaMath/Interpolations.jl/blob/master/doc/Interpolations.jl.ipynb
对于 Dierckx.jl:
https://github.com/kbarbary/Dierckx.jl
所以我尝试使用不同的函数来实验插值,例如: 一个简单的代码:
using Interpolations
xs = 0:5
f(x) = x^2 * abs(sin(3*x) + cos(x))
ys = f.(xs)
f_int = interpolate(ys, BSpline(Quadratic(Line(OnCell()))))
println("f(3.2) = ", f(3.2))
println("f_int(3.2) = ", f_int(3.2))
Run Code Online (Sandbox Code Playgroud)
二次插值应该是相当准确的,但结果如下:
f(3.2) = 12.007644743861604
f_int(3.2) = 2.973832923722435
Run Code Online (Sandbox Code Playgroud)
那么我对 Interpolations.jl 的功能有什么误解呢?Interpolations.jl 中的函数interpolate不接受数组xs作为参数,而只ys接受 ,所以我认为这可能是由于我“不正确”选择了xs?
然后我切换到 Dierckx.jl,它接受函数and中的xsand 。在我看来,在上面的示例中效果很好,因为我将函数插值行切换为:ysSpline1DSpline2DSpline1D
f_int = Spline1D(xs, ys)
Run Code Online (Sandbox Code Playgroud)
然而,当我尝试2D时,问题又出现了:
using Dierckx
xs = 1:5 …Run Code Online (Sandbox Code Playgroud) 我有一堆图像(3D 阵列),我想提高它们的分辨率(上采样)。我运行以下代码片段,我发现它有点慢......
有什么办法可以提高这段代码的速度吗?(不使用多处理)
using BenchmarkTools
using Interpolations
function doInterpol(arr::Array{Int, 2}, h, w)
A = interpolate(arr, BSpline(Linear()))
return A[1:2/(h-1)/2:2, 1:2/(w-1)/2:2]
end
function applyResize!(arr3D_hd::Array, arr3D_ld::Array, t::Int, h::Int, w::Int)
for i = 1:1:t
@inbounds arr3D_hd[i, :, :] = doInterpol(arr3D_ld[i, :, :], h, w)
end
end
t, h, w = 502, 65, 47
h_target, w_target = 518, 412
arr3D_ld = reshape(collect(1:t*h*w), (t, h, w))
arr3D_hd = Array{Float32}(undef, t, h_target, w_target)
applyResize!(arr3D_hd, arr3D_ld, t, h_target, w_target)
Run Code Online (Sandbox Code Playgroud)
当我对以下内容进行基准测试时:
@btime applyResize!(arr3D_hd, arr3D_ld, t, h_target, w_target)
Run Code Online (Sandbox Code Playgroud)
我有 : …
interpolation image image-processing multidimensional-array julia
我有一个谷歌存储桶对象,它是通过 terraform 创建的,如下所示:
resource "google_storage_bucket_object" "my_post_startup_script" {
name = "my-script.sh"
source = "${path.module}/my-script.sh"
bucket = my_bucket
}
Run Code Online (Sandbox Code Playgroud)
但在该脚本内部是我想要创建变量的东西。
所以说 my-script.sh 看起来像:
#!/bin/bash
echo "hello ${name}"
Run Code Online (Sandbox Code Playgroud)
有没有办法让我传递一个变量以便进行插值,以便上传的脚本实际上说“你好约翰”
这可能需要一些中间步骤,创建一个带有变量插值的文件,然后我可以将其作为 google_storage_bucket_object 的源传入 - 但不确定如何完成。
我使用 Numba 来加速我的代码。它效果很好,可提供 2-3 倍的改进。然而,我的代码中花费的主要时间(大约 90%)是在 scipy 四边形积分和插值(线性和三次样条)上。我进行了数百次这些集成,因此我认为这是 Numba 可以增强的功能。看起来 Numba 不支持这些?我听说过 Numba-Scipy ,它应该让 Numba 识别 Scipy,但这似乎仍然不起作用。有没有办法让 Numba 优化我的积分/插值?
interpolation ×10
julia ×2
scipy ×2
animation ×1
arrays ×1
derivative ×1
gam ×1
image ×1
integration ×1
javascript ×1
loops ×1
lua ×1
matplotlib ×1
mgcv ×1
numba ×1
python ×1
python-3.x ×1
r ×1
spline ×1
svg ×1
templating ×1
terraform ×1
variables ×1
vue.js ×1