Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add two numeric arrays in F#

I am totally new to F#. I have searched high and low but I cannot find an example for what I want.

let A = [| 1.0, 2.0, 3.0, 4.0 |];; //maybe delimiter with ;
let B = [| 4.0, 3.5, 2.5, 0.5 |];;

let C = A + B;; //how do I define the addition operator for arrays?
// expect C=[| 5.0, 5.5, 5.5, 4.5 |]

I have come close with this posting, but it is not what I want.

like image 242
John Alexiou Avatar asked Sep 14 '11 18:09

John Alexiou


People also ask

How do you add two arrays in numbers?

The idea is to start traversing both the array simultaneously from the end until we reach the 0th index of either of the array. While traversing each elements of array, add element of both the array and carry from the previous sum. Now store the unit digit of the sum and forward carry for the next index sum.

How do I combine two arrays?

In order to merge two arrays, we find its length and stored in fal and sal variable respectively. After that, we create a new integer array result which stores the sum of length of both arrays. Now, copy each elements of both arrays to the result array by using arraycopy() function.

How do you sum two arrays in Python?

To add the two arrays together, we will use the numpy. add(arr1,arr2) method. In order to use this method, you have to make sure that the two arrays have the same length. If the lengths of the two arrays are​ not the same, then broadcast the size of the shorter array by adding zero's at extra indexes.


1 Answers

let inline (++) a b = Array.map2 (+) a b

let A = [| 1.0; 2.0; 3.0; 4.0 |];;
let B = [| 4.0; 3.5; 2.5; 0.5 |];;
let A1 = [| 1; 2; 3; 1 |];;
let B1 = [| 4; 3; 2; 1 |];;

let C = A ++ B
let C1 = A1 ++ B1
like image 94
desco Avatar answered Sep 30 '22 16:09

desco