UNPKG

mathball

Version:

A JavaScript library for Competitive Programming

105 lines (97 loc) 3.67 kB
'use strict'; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /*Complex Numbers * Use: const a = new M.Complex(real, imaginery); */ var validate = require('../validation/integer-object'); var Complex = function () { function Complex(r, i) { _classCallCheck(this, Complex); this.r = r; this.i = i; validate(this.r, 'Complex'); validate(this.i, 'Complex'); } _createClass(Complex, [{ key: 'abs', value: function abs() { return parseFloat(Math.sqrt(Math.pow(this.r, 2) + Math.pow(this.i, 2)).toFixed(2)); } }, { key: 'conjugate', value: function conjugate() { var img = -this.i; var obj = new Complex(this.r, img); return obj; } }, { key: 'add', value: function add(args) { validate(args, 'add'); if (Number.isInteger(args)) { var obj = new Complex(this.r + args, this.i); return obj; } else { var _obj = new Complex(this.r + args.r, this.i + args.i); return _obj; } } }, { key: 'sub', value: function sub(args) { validate(args, 'sub'); if (Number.isInteger(args)) { var obj = new Complex(this.r - args, this.i); return obj; } else { var _obj2 = new Complex(this.r - args.r, this.i - args.i); return _obj2; } } }, { key: 'multiply', value: function multiply(args) { validate(args, 'multiply'); if (Number.isInteger(args)) { var obj = new Complex(this.r * args, this.i * args); return obj; } else { var _obj3 = new Complex(this.r * args.r - this.i * args.i, this.r * args.i + args.r * this.i); return _obj3; } } }, { key: 'divide', value: function divide(args) { validate(args, 'divide'); if (Number.isInteger(args)) { var obj = new Complex(parseFloat(this.r / args), parseFloat(this.i / args)); return obj; } else { var dm = Math.pow(args.r, 2) + Math.pow(args.i, 2); var real = parseFloat(((this.r * args.r + this.i * args.im) / dm).toFixed(2)); var img = parseFloat(((args.r * this.i - this.r * args.i) / dm).toFixed(2)); var _obj4 = new Complex(real, img); return _obj4; } } }, { key: 're', get: function get() { return this.r; } }, { key: 'im', get: function get() { return this.i; } }, { key: 'conj', get: function get() { return this.conjugate(); } }]); return Complex; }(); module.exports = Complex;