@@ -5,41 +5,21 @@ public class Statistics extends ArrayFire {
55
66 static private native long afMeanWeighted (long ref , long weightsRef , int dim );
77
8- static private native double afMeanAll (long ref );
8+ static private native DoubleComplex afMeanAll (long ref );
99
10- static private native double afMeanAllWeighted (long ref , long weightsRef );
11-
12- static private native FloatComplex afMeanAllFloatComplex (long ref );
13-
14- static private native DoubleComplex afMeanAllDoubleComplex (long ref );
15-
16- static private native FloatComplex afMeanAllFloatComplexWeighted (long ref , long weightsRef );
17-
18- static private native DoubleComplex afMeanAllDoubleComplexWeighted (long ref , long weightsRef );
10+ static private native DoubleComplex afMeanAllWeighted (long ref , long weightsRef );
1911
2012 static private native long afVar (long ref , boolean isBiased , int dim );
2113
2214 static private native long afVarWeighted (long ref , long weightsRef , int dim );
2315
24- static private native double afVarAll (long ref , boolean isBiased );
25-
26- static private native double afVarAllWeighted (long ref , long weightsRef );
27-
28- static private native FloatComplex afVarAllFloatComplex (long ref , boolean isBiased );
16+ static private native DoubleComplex afVarAll (long ref , boolean isBiased );
2917
30- static private native DoubleComplex afVarAllDoubleComplex (long ref , boolean isBiased );
31-
32- static private native FloatComplex afVarAllFloatComplexWeighted (long ref , long weightsRef );
33-
34- static private native DoubleComplex afVarAllDoubleComplexWeighted (long ref , long weightsRef );
18+ static private native DoubleComplex afVarAllWeighted (long ref , long weightsRef );
3519
3620 static private native long afStdev (long ref , int dim );
3721
38- static private native double afStdevAll (long ref );
39-
40- static private native FloatComplex afStdevAllFloatComplex (long ref );
41-
42- static private native DoubleComplex afStdevAllDoubleComplex (long ref );
22+ static private native DoubleComplex afStdevAll (long ref );
4323
4424 static public Array mean (final Array in , int dim ) {
4525 return new Array (afMean (in .ref , dim ));
@@ -50,43 +30,13 @@ static public Array mean(final Array in, final Array weights, int dim) {
5030 }
5131
5232 static public <T > T mean (final Array in , Class <T > type ) throws Exception {
53- if (type == FloatComplex .class ) {
54- FloatComplex res = (FloatComplex ) afMeanAllFloatComplex (in .ref );
55- return type .cast (res );
56- } else if (type == DoubleComplex .class ) {
57- DoubleComplex res = (DoubleComplex ) afMeanAllDoubleComplex (in .ref );
58- return type .cast (res );
59- }
60-
61- double res = afMeanAll (in .ref );
62- if (type == Float .class ) {
63- return type .cast (Float .valueOf ((float ) res ));
64- } else if (type == Double .class ) {
65- return type .cast (Double .valueOf ((double ) res ));
66- } else if (type == Integer .class ) {
67- return type .cast (Integer .valueOf ((int ) res ));
68- }
69- throw new Exception ("Unknown type" );
33+ DoubleComplex res = afMeanAll (in .ref );
34+ return castResult (res , type );
7035 }
7136
7237 static public <T > T mean (final Array in , final Array weights , Class <T > type ) throws Exception {
73- if (type == FloatComplex .class ) {
74- FloatComplex res = (FloatComplex ) afMeanAllFloatComplexWeighted (in .ref , weights .ref );
75- return type .cast (res );
76- } else if (type == DoubleComplex .class ) {
77- DoubleComplex res = (DoubleComplex ) afMeanAllDoubleComplexWeighted (in .ref , weights .ref );
78- return type .cast (res );
79- }
80-
81- double res = afMeanAllWeighted (in .ref , weights .ref );
82- if (type == Float .class ) {
83- return type .cast (Float .valueOf ((float ) res ));
84- } else if (type == Double .class ) {
85- return type .cast (Double .valueOf ((double ) res ));
86- } else if (type == Integer .class ) {
87- return type .cast (Integer .valueOf ((int ) res ));
88- }
89- throw new Exception ("Unknown type" );
38+ DoubleComplex res = afMeanAllWeighted (in .ref , weights .ref );
39+ return castResult (res , type );
9040 }
9141
9242 static public Array var (final Array in , boolean isBiased , int dim ) {
@@ -98,66 +48,40 @@ static public Array var(final Array in, final Array weights, int dim) {
9848 }
9949
10050 static public <T > T var (final Array in , boolean isBiased , Class <T > type ) throws Exception {
101- if (type == FloatComplex .class ) {
102- FloatComplex res = (FloatComplex ) afVarAllFloatComplex (in .ref , isBiased );
103- return type .cast (res );
104- } else if (type == DoubleComplex .class ) {
105- DoubleComplex res = (DoubleComplex ) afVarAllDoubleComplex (in .ref , isBiased );
106- return type .cast (res );
107- }
108-
109- double res = afVarAll (in .ref , isBiased );
110- if (type == Float .class ) {
111- return type .cast (Float .valueOf ((float ) res ));
112- } else if (type == Double .class ) {
113- return type .cast (Double .valueOf ((double ) res ));
114- } else if (type == Integer .class ) {
115- return type .cast (Integer .valueOf ((int ) res ));
116- }
117- throw new Exception ("Unknown type" );
51+ DoubleComplex res = afVarAll (in .ref , isBiased );
52+ return castResult (res , type );
11853 }
11954
12055 static public <T > T var (final Array in , final Array weights , Class <T > type ) throws Exception {
121- if ( type == FloatComplex . class ) {
122- FloatComplex res = ( FloatComplex ) afVarAllFloatComplexWeighted ( in . ref , weights . ref );
123- return type . cast ( res );
124- } else if ( type == DoubleComplex . class ) {
125- DoubleComplex res = ( DoubleComplex ) afVarAllDoubleComplexWeighted ( in . ref , weights . ref );
126- return type . cast ( res );
127- }
56+ DoubleComplex res = afVarAllWeighted ( in . ref , weights . ref );
57+ return castResult ( res , type );
58+ }
59+
60+ static public Array stdev ( final Array in , int dim ) {
61+ return new Array ( afStdev ( in . ref , dim ) );
62+ }
12863
129- double res = afVarAllWeighted (in .ref , weights .ref );
64+ static public <T > T stdev (final Array in , Class <T > type ) throws Exception {
65+ DoubleComplex res = afStdevAll (in .ref );
66+ return castResult (res , type );
67+ }
68+
69+ static public <T > T castResult (DoubleComplex res , Class <T > type ) throws Exception {
70+ Object ret ;
13071 if (type == Float .class ) {
131- return type . cast ( Float .valueOf ((float ) res ));
72+ ret = Float .valueOf ((float ) res . real ( ));
13273 } else if (type == Double .class ) {
133- return type . cast ( Double .valueOf ((double ) res ));
74+ ret = Double .valueOf ((double ) res . real ( ));
13475 } else if (type == Integer .class ) {
135- return type . cast ( Integer .valueOf ((int ) res ));
136- }
137- throw new Exception ( "Unknown type" );
138- }
139-
140- static public Array stdev ( final Array in , int dim ) {
141- return new Array ( afStdev ( in . ref , dim ) );
76+ ret = Integer .valueOf ((int ) res . real ( ));
77+ } else if ( type == FloatComplex . class ) {
78+ ret = new FloatComplex (( float ) res . real (), ( float ) res . imag () );
79+ } else if ( type == DoubleComplex . class ) {
80+ ret = res ;
81+ } else {
82+ throw new Exception ( "Unknown type" );
14283 }
14384
144- static public <T > T stdev (final Array in , Class <T > type ) throws Exception {
145- if (type == FloatComplex .class ) {
146- FloatComplex res = (FloatComplex )afStdevAllFloatComplex (in .ref );
147- return type .cast (res );
148- } else if (type == DoubleComplex .class ) {
149- DoubleComplex res = (DoubleComplex )afStdevAllDoubleComplex (in .ref );
150- return type .cast (res );
151- }
152-
153- double res = afStdevAll (in .ref );
154- if (type == Float .class ) {
155- return type .cast (Float .valueOf ((float ) res ));
156- } else if (type == Double .class ) {
157- return type .cast (Double .valueOf ((double ) res ));
158- } else if (type == Integer .class ) {
159- return type .cast (Integer .valueOf ((int ) res ));
160- }
161- throw new Exception ("Unknown type" );
162- }
85+ return type .cast (ret );
86+ }
16387}
0 commit comments