Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 从一个函数返回不同维度的数组;在F#中可能吗?_Arrays_F# - Fatal编程技术网

Arrays 从一个函数返回不同维度的数组;在F#中可能吗?

Arrays 从一个函数返回不同维度的数组;在F#中可能吗?,arrays,f#,Arrays,F#,具体来说,我正在尝试将一些Python转换为F# 该函数接受数量可变的int参数,并根据参数的数量返回不同维度的数组 我认为这是不可能的,因为一个函数不能返回不同的类型(int[],int[][]],int[][][],等等),除非它们是有区别的联合的一部分,但在提交解决方案之前要确保 健全性检查: member self.differntarrays ([<ParamArray>] dimensions: Object[]) = match dimensions with

具体来说,我正在尝试将一些Python转换为F#

该函数接受数量可变的int参数,并根据参数的数量返回不同维度的数组

我认为这是不可能的,因为一个函数不能返回不同的类型(
int[]
int[][]]
int[][][]
,等等),除非它们是有区别的联合的一部分,但在提交解决方案之前要确保

健全性检查:

member self.differntarrays ([<ParamArray>] dimensions: Object[]) =
    match dimensions with
    | [| dim1 |] ->      
         [| 
            1 
         |]
    | [| dim1; dim2 |] -> 
        [|
           [| 2 |], 
           [| 3 |] 
        |]
    | _ -> failwith "error"
表达式为:
[| 2 |],[| 3 |]

int
指的是
[|1 |]

i、 e.
1
的类型与
[| 2 |]、[| 3 |]

TLDR

numpy.random.randn(d0,d1,…,dn)

从“标准正态”分布返回一个或多个样本

如果提供了正参数、类int_参数或可转换int参数,则randn 生成一个形状数组(d0,d1,…,dn),填充随机变量 从一元“正态”(高斯)分布采样的浮点数 平均值0和方差1(如果任何一个d_i是浮动,则它们是第一个 通过截断转换为整数)。随机抽样的单个浮点数 如果未提供参数,则返回来自分发的

交互式python会话的示例:

np.random.randn(1)  - array([-0.28613356])
np.random.randn(2)  - array([-1.7390449 ,  1.03585894]) 
np.random.randn(1,1)- array([[ 0.04090027]])
np.random.randn(2,3)- array([[-0.16891324,  1.05519898, 0.91673992],  
                             [ 0.86297031,  0.68029926, -1.0323683 ]])

代码用于,由于性能原因,值需要可变,因此使用不可变列表不是一个选项。

您是正确的-浮点数组
float[]
与浮点数组的类型不同
float[][]
或二维浮点数组
float[,]
,因此无法编写根据输入参数返回一个或另一个的函数

如果您想执行类似Python的
rand
,可以编写一个重载方法:

type Random() = 
  static let rnd = System.Random()
  static member Rand(n) = [| for i in 1 .. n -> rnd.NextDouble() |]
  static member Rand(n1, n2) = [| for i in 1 .. n1 -> Random.Rand(n2) |]
  static member Rand(n1, n2, n3) = [| for i in 1 .. n1 -> Random.Rand(n2, n3) |]

尽管Tomas建议使用重载可能是最好的,.NET数组确实共享一个共同的子类型:
System.Array
。所以你想要的是可能的

member self.differntarrays ([<ParamArray>] dimensions: Object[]) : Array =
    match dimensions with
    | [| dim1 |] ->      
         [| 
            1 
         |] :> _
    | [| dim1; dim2 |] -> 
        [|
           [| 2 |], 
           [| 3 |] 
        |] :> _
    | _ -> failwith "error"
member self.differntarray([]维度:对象[]):数组=
将尺寸与
|[| dim1 |]->
[| 
1.
|] :> _
|[dim1;dim2]->
[|
[| 2 |], 
[| 3 |] 
|] :> _
|->failwith“error”

您需要使用与DU:member self.differntarrays([]维度:Object[]):Object[]不同的解决方法的DUMight,注意它返回Object[]。这可能会导致下游出现问题,因此尚未提交。请注意:谢谢。我总是很欣赏一个不同的答案,因为它能为未来的问题提供思路,让头脑保持工作状态。
member self.differntarrays ([<ParamArray>] dimensions: Object[]) : Array =
    match dimensions with
    | [| dim1 |] ->      
         [| 
            1 
         |] :> _
    | [| dim1; dim2 |] -> 
        [|
           [| 2 |], 
           [| 3 |] 
        |] :> _
    | _ -> failwith "error"