cpdef myf():
# pd has to be a c array.
# Because it will then be consumed by some c function.
cdef double pd[8000]
# Do something with pd
...
# Get a memoryview.
cdef double[:] pd_view = pd
# Coercion the memoryview to numpy array. Not working.
ret = np.asarray(pd)
return ret
Run Code Online (Sandbox Code Playgroud)
我希望它返回一个numpy数组.我该怎么做?
目前我必须这样做
pd_np = np.zeros(8000, dtype=np.double)
cdef int i
for i in range(8000):
pd_np[i] = pd[i]
Run Code Online (Sandbox Code Playgroud) 在单击数据点时,所有其他数据点都被加阴影。有办法防止这种情况吗?
fig = fig.circle(x, y)
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想增加所选圆的大小。有一个简单的原因吗?
似乎我们不能更改大小...根据这里:
渲染时仅考虑selection_glyph和nonselection_glyph的视觉属性。更改位置,大小等均无效。
但是,我们可以使用line_width属性对其进行仿真,如果将其与结合使用,它将变得更加有趣line_dish。
为了实例化类似的类型 x = MyType{Int}()
我可以定义一个内部构造函数.
immutable MyType{T}
x::Vector{T}
MyType() = new(T[])
end
Run Code Online (Sandbox Code Playgroud)
是否可以使用外部构造函数实现相同的目标?
说,我有一个类型层次结构
abstract A
immutable B <: A end
immutable C <: A end
Run Code Online (Sandbox Code Playgroud)
A以下工厂模式的构造函数:
function A(x::Int)
if x > 0
B()
else
C()
end
end
Run Code Online (Sandbox Code Playgroud)
它根据预期的输入返回不同的子类型.但是,它也是类型不稳定,因为我找不到强制返回类型的方法A.
那么,这里有工厂模式是不是很糟糕?类型不稳定性是否仅影响不可变类型而不是可变类型,因为后者无论如何都是引用类型.
我必须为此选择参数类型吗?
immutable D{T <: A}
type::T
end
function D(x::Int)
if x > 0
D(B())
else
D(C())
end
end
Run Code Online (Sandbox Code Playgroud)
感觉有点糟糕.
实际上,类型不稳定的功能有多糟糕?是否值得交易以获得更好的代码可读性?
或者,我应该定义typealias A Union{B,C}吗?
function memoize(f, T)
cache = Dict{Any, T}()
function g(args...)::T
key = make_key(args...)
get!(cache, key) do
f(args...)
end
end
g
end
fib = memoize(x::Int -> begin
if x == 2
return 2
end
if x == 1
return 1
end
fib(x - 1) + fib(x - 2)
end, Int)
Run Code Online (Sandbox Code Playgroud)
这就是我得到的,遗憾的是,虽然我注释了它不能识别返回类型.
另外,有没有办法注释匿名函数的返回类型?
@code_warntype fib(3)
Variables:
#self#::#g#40{##44#45,DataType,Dict{Any,Int64}}
args::Tuple{Int64}
key::Tuple{Int64}
#39::##39#41{Tuple{Int64},##44#45}
Body:
begin
SSAValue(0) = (Core.getfield)(#self#::#g#40{##44#45,DataType,Dict{Any,Int64}},:T)::DataType
key::Tuple{Int64} = (Core.tuple)((Core.getfield)(args::Tuple{Int64},1)::Int64)::Tuple{Int64} # line 20:
#39::##39#41{Tuple{Int64},##44#45} = $(Expr(:new, ##39#41{Tuple{Int64},##44#45}, :(args), :((Core.getfield)(#self#,:f)::##44#45)))
SSAValue(1) = #39::##39#41{Tuple{Int64},##44#45}
SSAValue(2) = (Core.getfield)(#self#::#g#40{##44#45,DataType,Dict{Any,Int64}},:cache)::Dict{Any,Int64} …Run Code Online (Sandbox Code Playgroud) 额外的语法被注入到作用域中markup.fenced_code.block.markdown
不过L:markup.fenced_code.block.markdown是用的。在这种情况下意味着什么L:?它是范围选择器的特殊语法吗?我在https://manual.macromates.com/en/scope_selectors.html中找不到任何相关信息
我们知道文件的哈希值与文件名无关.我做了一些实验,证明了在mac os方面,文件的标签(红色,...),关键字,描述(在开放式元数据中)的更改不会改变哈希值.
但jpeg中元数据的更改确实会改变哈希值.
所以我开始想知道为什么它持有?任何线索或鼓舞人心的教程?
由于C#不能很好地支持递归调用的优化(例如尾递归).看来我必须将代码风格从函数式编程转换为我不熟悉的东西.
我知道有时迭代方法替代存在于递归方法中,但通常很难找到有效的方法.
现在,一般来说,我相信所有递归方法都可以通过stack<T>某种方式使用数据结构来重写.
我在哪里可以找到教程或介绍或一般规则?
例如,如果我想重写最常见的除数方法怎么办?给定m> n
int gcd(int m, int n)
{
if (n==0)
return m;
else
return gcd(n,m%n);
}
Run Code Online (Sandbox Code Playgroud)
更新
可能它是一个坏的例子,因为它确实是尾递归.Plz只是忽略了这个事实并认为它是一种正常的递归方法.
我使用DotTrace 4.5性能
发布模式下的时间:
2400000000
Basic: 00:00:08.8051103
2400000000
Five: 00:00:09.1561338
2400000000
Overload: 00:00:16.3740938
2400000000
IListtoFive: 00:00:15.5841445
Run Code Online (Sandbox Code Playgroud)
在发布模式下进行性能分析的时间.
2400000000
Basic: 00:00:01.0048224
2400000000
Five: 00:00:03.5416982
2400000000
Overload: 00:00:11.8009959
2400000000
IListtoFive: 00:00:11.2568770
Run Code Online (Sandbox Code Playgroud)
我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace testLineIndex
{
class Program
{
static long Five(int s0, int s1, int s2, int s3, int s4)
{
return s4 + 100 * s3 + 10000 * s2 + 1000000 * s1 + 100000000 * s0;
}
static long Overload(IList<int> …Run Code Online (Sandbox Code Playgroud) 我想将一个整数数组与以下伪模式匹配:其中a表示这些数字是等于或0(即假设任何数字"等于"0)
[| a; a; a; a; a; |] // matches [| 1; 1; 0; 0; 1 |]
[| a; a; a; not a; a; |] // matches [| 3; 3; 0; 2; 3 |]
[| a; a; not a; a; a; |] // matches [| 4; 4; 2; 0; 4 |]
[| a; not a; a; a; not a; |] // matches [| 1; 2; 0; 0; 3 |]
[| a; a; a; not a; a; |] // …Run Code Online (Sandbox Code Playgroud) julia ×3
c# ×2
bokeh ×1
cython ×1
dottrace ×1
f# ×1
filesystems ×1
hash ×1
numpy ×1
performance ×1
profiling ×1
python ×1
textmate ×1
tmlanguage ×1