在F#中,有NativePtr模块,但它似乎只为其'add/get/set函数支持32位偏移,就像System.IntPtr一样.
有没有办法在F#中为本机指针(nativeptr <'a>)添加64位偏移量?当然,我可以将所有地址转换为64位整数,执行正常的整数运算,然后将结果再次转换为nativeptr <'a>,但这会花费额外的add和imul指令.我真的希望AGU执行地址计算.
例如,在C#中使用unsafe,你可以做类似的事情
void* ptr = Marshal.AllocHGlobal(...).ToPointer();
int64 offset = ...;
T* newAddr = (T*)ptr + offset; // T has to be an unmanaged type
Run Code Online (Sandbox Code Playgroud)
实际上你不能,因为类型参数没有"非托管"约束,但至少你可以用非泛型方式进行通用指针运算.
在F#中,我们终于得到了非托管约束; 但是如何进行指针运算呢?
我很难将 F# 测量单位与System.Numerics.Vector<'T>类型结合使用。让我们看一个玩具问题:假设我们有一个类型xs的数组float<m>[],并且由于某种原因我们想要对其所有组件进行平方,从而得到一个类型的数组float<m^2>[]。这与标量代码完美配合:
xs |> Array.map (fun x -> x * x) // float<m^2>[]
Run Code Online (Sandbox Code Playgroud)
现在假设我们想要通过使用 SIMD 在 - 大小的块中执行乘法来向量化此操作System.Numerics.Vector<float>.Count,例如如下所示:
open System.Numerics
let simdWidth = Vector<float>.Count
// fill with dummy data
let xs = Array.init (simdWidth * 10) (fun i -> float i * 1.0<m>)
// array to store the results
let rs: float<m^2> array = Array.zeroCreate (xs |> Array.length)
// number of SIMD operations required
let chunks = (xs |> …Run Code Online (Sandbox Code Playgroud) 到目前为止,我对F#中的类型推断给我留下了深刻的印象,但是我找到了一些它没有真正得到的东西:
//First up a simple Vect3 type
type Vect3 = { x:float; y:float; z:float } with
static member (/) (v1 : Vect3, s : float) = //divide by scalar, note that float
{x=v1.x / s; y= v1.y /s; z = v1.z /s}
static member (-) (v1 : Vect3, v2 : Vect3) = //subtract two Vect3s
{x=v1.x - v2.x; y= v1.y - v2.y; z=v1.z - v2.z}
//... other operators...
//this works fine
let floatDiff h (f: float -> float) x = …Run Code Online (Sandbox Code Playgroud) 是否可以定义一个MATLAB类,以便可以像调用任何其他函数一样调用此类中的对象?
IOW,我问的是,是否可以在MATLAB中编写类似以下Python类的内容:
# define the class FxnClass
class FxnClass(object):
def __init__(self, template):
self.template = template
def __call__(self, x, y, z):
print self.template % locals()
# create an instance of FxnClass
f = FxnClass('x is %(x)r; y is %(y)r; z is %(z)r')
# call the instance of FxnClass
f(3, 'two', False)
...
[OUTPUT]
x is 3; y is 'two'; z is False
Run Code Online (Sandbox Code Playgroud)
谢谢!
我想创建一个函数,将某个类的所有名称作为字符串列表返回.基于之前的解决方案/问题,我尝试使用此代码但没有成功
function GetClassElementNames (TObject ) : TStringlist ;
var
LCtx : TRttiContext;
LMethod : TRttiMethod;
begin
try
LCtx:=TRttiContext.Create;
try
// list the methods for the any class class
for LMethod in LCtx.GetType(TObject).GetMethods do
result.add(LMethod.Name);
finally
LCtx.Free;
end;
except
on E: Exception do
result.add (E.ClassName + ': ' + E.Message);
end;
end;
Run Code Online (Sandbox Code Playgroud) 首次使用F#进行生产,需要一些帮助.请参阅此代码,我在每行添加了警告作为注释:
type AssetClass =
| Corp
| Corp_SME
| Res_Mort
| Qual_Ret
| Ret_Oth
let Correlation assetClass pd sales =
match assetClass with
| Corp -> 0.12
| CORP_SME -> 0.24 // warning FS0049: Uppercase variable identifiers
| Res_Mort -> 0.15 // warning FS0026: This rule will never be matched
| Qual_Ret -> 0.04 // warning FS0026: This rule will never be matched
| Ret_Oth -> 0.03 // warning FS0026: This rule will never be matched
Run Code Online (Sandbox Code Playgroud)
我检查了它并没有虚张声势,第三个和其他情况确实被忽略了.我没有到这里来的是什么?(我在实际实现中使用的pd和销售输入,我在这里省略了公式.)
我想要做的是使用区分联合,因为我将在C#中使用枚举,然后打开它.所以在C#中我会输入这个:
enum AssetClass …Run Code Online (Sandbox Code Playgroud) 这是一个简单的问题,我正在实现后缀数组,但我被困在这里:
#define SIZE 150
struct node{
transition *next[SIZE]; //error here
};
struct transition{
int left, right;
node *suffix_link;
};
Run Code Online (Sandbox Code Playgroud)
这段代码不会编译,第三行有错误,任何人都可以帮我PLZ吗?感谢:D
更新:我的不好我忘了包括第一行,对不起,这是我的第一个问题:P
与F#进行模式匹配时遇到问题.我正在构建一个F#库,到目前为止这个:
namespace parser
module parse =
let public y = function
| x when x.Contains("hi") -> "HELLO"
| x when x.Contains("hello") -> "HI"
| x -> x
Run Code Online (Sandbox Code Playgroud)
但它给了我错误:根据此程序点之前的信息查找不确定类型的对象的错误.在此程序点之前可能需要类型注释来约束对象的类型.这可以允许解析查找.