我正在使用Sinatra并使用该get '/foo/:bar' {}方法从url获取参数.不幸的是,由于没有路由匹配/ ,因此其中的值:bar可能包含/导致404的讨厌内容/foo/:bar/baz.我URI.escape用来转义URL参数,但它认为/有效的有效字符.正如这里提到的那样,这是因为要检查的默认Regexp不区分不安全字符和保留字符.我想改变这个并做到这一点:
URI.escape("foo_<_>_&_3_#_/_+_%_bar", Regexp.union(URI::REGEXP::UNSAFE, '/'))
Run Code Online (Sandbox Code Playgroud)
只是为了测试它.
URI::REGEXP::UNSAFE是根据Ruby 1.9.3 Documentaton匹配的默认正则表达式:
escape(*arg)
Synopsis
URI.escape(str [, unsafe])
Args
str
String to replaces in.
unsafe
Regexp that matches all symbols that must be replaced with
codes. By default uses REGEXP::UNSAFE. When this argument is
a String, it represents a character set.
Description
Escapes the string, replacing all unsafe characters with codes.
Run Code Online (Sandbox Code Playgroud)
不幸的是我收到了这个错误:
uninitialized …Run Code Online (Sandbox Code Playgroud) 我有一个将向量映射到向量的函数
我想计算它的雅可比行列式
,
其中雅可比行列式定义为
。
因为我可以使用numpy.linalg.det, 来计算行列式,所以我只需要雅可比矩阵。我知道numdifftools.Jacobian,但这使用数值微分,而我追求自动微分。输入Autograd/ (我现在JAX会坚持使用,它有一个方法,但只要我得到我想要的,我就很乐意使用)。如何与向量值函数一起正确使用此函数?Autogradautograd.jacobian()JAXautograd.jacobian()
作为一个简单的例子,我们来看一下这个函数
 2C%20x_1%5E2%29 )
其中有雅可比行列式

产生雅可比行列式
>>> import autograd.numpy as np
>>> import autograd as ag
>>> x = np.array([[3],[11]])
>>> result = 4*x[0]*x[1]
array([132])
>>> jac = ag.jacobian(f)(x)
array([[[[ 6],
[ 0]]],
[[[ 0],
[22]]]])
>>> jac.shape
(2, 1, 2, 1)
>>> np.linalg.det(jac)
Traceback …Run Code Online (Sandbox Code Playgroud)