小编Phi*_*han的帖子

Ruby 1.9.3将不安全的字符添加到URI.escape

我正在使用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)

ruby uri escaping sinatra ruby-1.9.3

3
推荐指数
1
解决办法
2431
查看次数

使用 Python JAX/Autograd 计算向量值函数的雅可比行列式

我有一个将向量映射到向量的函数

f: R^n -> R^n

我想计算它的雅可比行列式

det J = |df/dx|,

其中雅可比行列式定义为

J_ij = |df_i/dx_j|

因为我可以使用numpy.linalg.det, 来计算行列式,所以我只需要雅可比矩阵。我知道numdifftools.Jacobian,但这使用数值微分,而我追求自动微分。输入Autograd/ (我现在JAX会坚持使用,它有一个方法,但只要我得到我想要的,我就很乐意使用)。如何与向量值函数一起正确使用此函数?Autogradautograd.jacobian()JAXautograd.jacobian()

作为一个简单的例子,我们来看一下这个函数

![f(x)=(x_0^2, x_1^2)]( https://chart.googleapis.com/chart?cht=tx&chl=f(x%29%20%3D%20(x_0%5E2% ) 2C%20x_1%5E2%29 )

其中有雅可比行列式

![J_f = diag(2 x_0, 2 x_1)]( https://chart.googleapis.com/chart?cht=tx&chl=J_f%20%3D%20%5合作者名称%7Bdiag%7D(2x_0%2C%202x_1% 29

产生雅可比行列式

检测 J_f = 4 x_0 x_1

>>> 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)

python numpy automatic-differentiation jax

3
推荐指数
1
解决办法
3038
查看次数