@@ -171,10 +171,7 @@ public static float min(float[] arr) {
171171 * @return result array
172172 */
173173 public static float [] plus (float [] src , float op , float @ Nullable [] res ) {
174- if (res == null ) res = new float [src .length ];
175- int n = Math .min (src .length , res .length );
176- for (int i = 0 ; i < n ; i ++) res [i ] = src [i ] + op ;
177- return res ;
174+ return applyEach (src , v -> v + op , res );
178175 }
179176
180177 /**
@@ -186,11 +183,7 @@ public static float[] plus(float[] src, float op, float @Nullable [] res) {
186183 * @return result array
187184 */
188185 public static float [] plus (float [] src , float [] op , float @ Nullable [] res ) {
189- if (src .length != op .length ) throw new IllegalArgumentException ("Can't add arrays of different size." );
190- if (res == null ) res = new float [src .length ];
191- int n = Math .min (src .length , res .length );
192- for (int i = 0 ; i < n ; i ++) res [i ] = src [i ] + op [i ];
193- return res ;
186+ return applyEach (src , op , Float ::sum , res );
194187 }
195188
196189 /**
@@ -202,16 +195,21 @@ public static float[] plus(float[] src, float[] op, float @Nullable [] res) {
202195 * @return result array
203196 */
204197 public static float [] mult (float [] src , float op , float @ Nullable [] res ) {
205- if (res == null ) res = new float [src .length ];
206- int n = Math .min (src .length , res .length );
207- for (int i = 0 ; i < n ; i ++) res [i ] = src [i ] * op ;
208- return res ;
198+ return applyEach (src , v -> v * op , res );
199+ }
200+
201+ public static float [] mult (float [] src , float [] op , float @ Nullable [] res ) {
202+ return applyEach (src , op , (v , op1 ) -> v * op1 , res );
209203 }
210204
211205 public static float [] div (float [] src , float op , float @ Nullable [] res ) {
212206 return mult (src , 1 / op , res );
213207 }
214208
209+ public static float [] div (float [] src , float [] op , float @ Nullable [] res ) {
210+ return applyEach (src , op , (v , op1 ) -> v / op1 , res );
211+ }
212+
215213 public static float [] diff (float [] src ) {
216214 if (src .length < 2 ) return EMPTY ;
217215 if (src .length == 2 ) return new float []{src [1 ] - src [0 ]};
@@ -247,6 +245,16 @@ public static float[] applyEach(float[] src, float[] operands1, float[] operands
247245 return res ;
248246 }
249247
248+ public static float [] applyEach (float [] src , float [][] operands , NFloatOperator op , float @ Nullable [] res ) {
249+ if (src .length != operands .length ) {
250+ throw new IllegalArgumentException ("Can't apply operator to operands of different size." );
251+ }
252+ if (res == null ) res = new float [src .length ];
253+ int n = Math .min (src .length , res .length );
254+ for (int i = 0 ; i < n ; i ++) res [i ] = op .apply (src [i ], operands [i ]);
255+ return res ;
256+ }
257+
250258 public static float [] abs (float [] src , float @ Nullable [] res ) {
251259 return applyEach (src , Math ::abs , res );
252260 }
@@ -259,6 +267,10 @@ public static float[] cos(float[] src, float @Nullable [] res) {
259267 return applyEach (src , MathUtils ::cos , res );
260268 }
261269
270+ public static float [] tan (float [] src , float @ Nullable [] res ) {
271+ return applyEach (src , MathUtils ::tan , res );
272+ }
273+
262274 public static float [] clamp (float [] src , float min , float max , float @ Nullable [] res ) {
263275 return applyEach (src , v -> MathUtils .clamp (v , min , max ), res );
264276 }
@@ -291,4 +303,9 @@ public interface TernaryFloatOperator {
291303
292304 float apply (float v , float op1 , float op2 );
293305 }
306+
307+ public interface NFloatOperator {
308+
309+ float apply (float v , float [] op );
310+ }
294311}
0 commit comments