Skip to content

Commit

Permalink
Updated to v1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
tanaikech committed Jan 25, 2024
1 parent 8a6cfb1 commit be27e59
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ In order to use this library, please install the library as follows.
| [convArrayToObject](#convarraytoobject) | Converting 2 dimensional array to JSON object. |
| [unpivot](#unpivot) | Converting 2-dimensional array as unpivot (reverse pivot). |
| [reverseUnpivot](#reverseunpivot) | Reversing 2-dimensional array with unpivot. |
| [dotProduct](#dotproduct) | Calculate dot product from 2 arrays. |
| [cosineSimilarity](#cosinesimilarity) | Calculate cosine similarity from 2 arrays. |

## For binary processing

Expand Down Expand Up @@ -799,6 +801,68 @@ function reverseUnpivot(values) {
}
````

<a name="dotproduct"></a>

### dotProduct

Calculate dot product from 2 arrays.

```javascript
/**
* ### Description
* Calculate dot product from 2 arrays.
*
* ### Sample script
* ```
* const array1 = [1, 2, 3, 4, 5];
* const array2 = [3, 4, 5, 6, 7];
* const res = UtlApp.dotProduct(array1, array2);
* ```
*
* @param {Array} array1 1-dimensional array including numbers.
* @param {Array} array2 1-dimensional array including numbers.
* @return {Number} Calculated result of dot product.
*/
function dotProduct(array1, array2) {
if (!Array.isArray(array1) || !Array.isArray(array2)) {
throw new Error("Please give 2 arrays.");
}
return array1.reduce((t, e, i) => t += e * array2[i], 0);
}
```

<a name="cosinesimilarity"></a>

### cosineSimilarity

Calculate cosine similarity from 2 arrays.

```javascript
/**
* ### Description
* Calculate cosine similarity from 2 arrays.
*
* ### Sample script
* ```
* const array1 = [1, 2, 3, 4, 5];
* const array2 = [3, 4, 5, 6, 7];
* const res = UtlApp.cosineSimilarity(array1, array2);
* ```
*
* @param {Array} array1 1-dimensional array including numbers.
* @param {Array} array2 1-dimensional array including numbers.
* @return {Number} Calculated result of cosine similarity.
*/
function cosineSimilarity(array1, array2) {
if (!Array.isArray(array1) || !Array.isArray(array2)) {
throw new Error("Please give 2 arrays.");
}
const dotProduct = array1.reduce((t, e, i) => t += e * array2[i], 0);
const magnitudes = [array1, array2].map(e => Math.sqrt(e.reduce((t, f) => t += f * f, 0))).reduce((t, f) => t *= f, 1);
return dotProduct / magnitudes;
}
```

---

## For binary processing
Expand Down Expand Up @@ -1723,4 +1787,8 @@ I believe that these methods will help to develop the applications created by Go

1. From [this report](https://github.com/tanaikech/UtlApp/issues/1) by [Max-Makhrov](https://github.com/Max-Makhrov), a bug of `convA1NotationToGridRange` was removed.

- v1.0.2 (January 25, 2024)

1. 2 methods of [dotProduct](#dotproduct) and [cosineSimilarity](#cosinesimilarity) were added.

[TOP](#top)
46 changes: 46 additions & 0 deletions forArrayProcessing.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,49 @@ function reverseUnpivot(values) {
const temp = [[null, ...rh], ...[...Array(Math.ceil(c.length / size))].map(_ => c.splice(0, size)).map((vv, i) => [ch[i], ...vv])];
return temp[0].map((_, c) => temp.map(r => r[c]));
}

/**
* ### Description
* Calculate dot product from 2 arrays.
*
* ### Sample script
* ```
* const array1 = [1, 2, 3, 4, 5];
* const array2 = [3, 4, 5, 6, 7];
* const res = UtlApp.dotProduct(array1, array2);
* ```
*
* @param {Array} array1 1-dimensional array including numbers.
* @param {Array} array2 1-dimensional array including numbers.
* @return {Number} Calculated result of dot product.
*/
function dotProduct(array1, array2) {
if (!Array.isArray(array1) || !Array.isArray(array2)) {
throw new Error("Please give 2 arrays.");
}
return array1.reduce((t, e, i) => t += e * array2[i], 0);
}

/**
* ### Description
* Calculate cosine similarity from 2 arrays.
*
* ### Sample script
* ```
* const array1 = [1, 2, 3, 4, 5];
* const array2 = [3, 4, 5, 6, 7];
* const res = UtlApp.cosineSimilarity(array1, array2);
* ```
*
* @param {Array} array1 1-dimensional array including numbers.
* @param {Array} array2 1-dimensional array including numbers.
* @return {Number} Calculated result of cosine similarity.
*/
function cosineSimilarity(array1, array2) {
if (!Array.isArray(array1) || !Array.isArray(array2)) {
throw new Error("Please give 2 arrays.");
}
const dotProduct = array1.reduce((t, e, i) => t += e * array2[i], 0);
const magnitudes = [array1, array2].map(e => Math.sqrt(e.reduce((t, f) => t += f * f, 0))).reduce((t, f) => t *= f, 1);
return dotProduct / magnitudes;
}

0 comments on commit be27e59

Please sign in to comment.