| 28 | * operation ==> The type of operation to perform |
| 29 | */ |
| 30 | export function _genericMathOp({ ndFrame, other, operation }: { |
| 31 | ndFrame: Series |
| 32 | other: Series | number | Array<number> |
| 33 | operation: string |
| 34 | }) { |
| 35 | if (typeof other === 'number') { |
| 36 | //broadcast operation |
| 37 | let newData; |
| 38 | switch (operation) { |
| 39 | case 'add': |
| 40 | newData = (ndFrame.values as number[]).map((ele => ele + other)) |
| 41 | return newData |
| 42 | |
| 43 | case 'sub': |
| 44 | newData = (ndFrame.values as number[]).map((ele => ele - other)) |
| 45 | return newData |
| 46 | |
| 47 | case 'mul': |
| 48 | newData = (ndFrame.values as number[]).map((ele => ele * other)) |
| 49 | return newData |
| 50 | |
| 51 | case 'div': |
| 52 | newData = (ndFrame.values as number[]).map((ele => ele / other)) |
| 53 | return newData |
| 54 | |
| 55 | case 'mod': |
| 56 | newData = (ndFrame.values as number[]).map((ele => ele % other)) |
| 57 | return newData |
| 58 | |
| 59 | case 'pow': |
| 60 | newData = (ndFrame.values as number[]).map((ele => ele ** other)) |
| 61 | return newData |
| 62 | |
| 63 | case 'minimum': |
| 64 | newData = (ndFrame.values as number[]).map((ele => Math.min(ele, other))) |
| 65 | return newData |
| 66 | |
| 67 | case 'maximum': |
| 68 | newData = (ndFrame.values as number[]).map((ele => Math.max(ele, other))) |
| 69 | return newData |
| 70 | |
| 71 | default: |
| 72 | throw new Error(`${operation} is not implemented`); |
| 73 | |
| 74 | } |
| 75 | } else if (other instanceof Series) { |
| 76 | utils.checkSeriesOpCompactibility({ firstSeries: ndFrame, secondSeries: other, operation }) |
| 77 | |
| 78 | let newData; |
| 79 | switch (operation) { |
| 80 | case 'add': |
| 81 | |
| 82 | newData = (ndFrame.values as number[]).map((ele, index) => { return ele + (other.values as number[])[index] }) |
| 83 | return newData |
| 84 | |
| 85 | case 'sub': |
| 86 | |
| 87 | newData = (ndFrame.values as number[]).map((ele, index) => { return ele - (other.values as number[])[index] }) |