diff --git a/examples/Astart.nr b/examples/Astart.nr new file mode 100644 index 0000000..2ff03bd --- /dev/null +++ b/examples/Astart.nr @@ -0,0 +1,179 @@ +/*############ A*(A-star) Algorithm ############## + + By @VictorKariuki + + https://github.com/VictorKariuki + +################################################*/ + + +// create a list of numbers +fanya list = unda(first,last,interval){ + fanya list = [first]; + fanya i = first + interval; + wakati(i < last){ + list.sukuma(i) + i+=interval; + } + rudisha list; +} + +// Maths functions +// find the absolute value of a number +fanya abs_namba = unda(namba){ + kama(namba < 0){ + rudisha -1 * namba; + } + + rudisha namba; +} + +// square a number +fanya square = unda(n, i, j){ + fanya kati = (i+j)/2; + fanya mul = kati * kati; + fanya abs_diff = abs_namba(mul-n); + + kama (mul == n || abs_diff < 0.00001){ + rudisha kati; + }au kama(mul < n){ + rudisha square(n,kati,j) + }au{ + rudisha square(n,i,kati) + } +} + +// find the square root of a number +fanya sqrt = unda(namba){ + kwa i ktk list(0,namba,1) { + kama((i*i )== namba){ + rudisha i; + }au kama ((i*i )> namba){ + rudisha square(namba,i-1,i) + } + } +} + +// Main function +fanya aStar = unda(start, goal) { + // Initialize the open and closed lists + fanya openList = [start]; + fanya closedList = []; + + fanya reconstructPath = unda(node) { + fanya path = [node]; + wakati (node["parent"]) { + path = [node["parent"]] + path; + node = node["parent"]; + } + rudisha path; + } + + fanya heuristic = unda(node1, node2) { + // Calculate the Euclidean distance between the nodes' positions + fanya dx = node1["x"] - node2["x"]; + fanya dy = node1["y"] - node2["y"]; + rudisha sqrt(dx * dx + dy * dy); + } + + fanya findMinNode = unda(openList) { + fanya i = 1; + fanya minNode = openList[0]; + + wakati (i < openList.idadi()) { + fanya node = openList[i]; + kama (node["f"] < minNode["f"]) { + minNode = node; + } + i++ + } + + rudisha minNode; + } + + fanya removeNodeFromArray = unda(array, node) { + fanya newArray = []; + fanya i = 1; + wakati (i < array.idadi()) { + kama (array[i] != node) { + newArray.sukuma(array[i]); + } + i++; + } + rudisha newArray; + } + + fanya urefu = unda(node1, node2) { + // Assume all edges have a cost of 1 + rudisha 1; + } + + // Initialize the g and f scores of the starting node + start["g"] = 0; + start["f"] = start["g"] + heuristic(start, goal); + + + + // Start the search loop + wakati (openList.idadi() > 0) { + // Find the node with the lowest f score in the open list + fanya current = findMinNode(openList); + + // Check kama the goal node has been reached + kama (current == goal) { + rudisha reconstructPath(current); + } + + // Move the current node from the open to the closed list + openList = removeNodeFromArray(openList, current); + + closedList.sukuma(current); + + // Explore the neighbors of the current node + kwa neighbor ktk current["neighbors"] { + // Skip neighbors that are in the closed list + kama (neighbor ktk closedList) { + endelea + } + + // Calculate the tentative g score of the neighbor + fanya tentativeG = start["g"] + urefu(current, neighbor); + + // Check kama the neighbor is in the open list + fanya tentativeIsBetter = sikweli; + kama (!(neighbor ktk openList)) { + openList.sukuma(neighbor); + tentativeIsBetter = kweli; + } au kama (tentativeG < neighbor["g"]) { + tentativeIsBetter = kweli; + } + + // Update the neighbor's g score kama the tentative score is better + kama (tentativeIsBetter) { + neighbor["g"] = tentativeG; + neighbor["f"] = neighbor["g"] + heuristic(neighbor, goal); + neighbor["parent"] = current; + } + } + } + + // kama the open list is empty, no path was found + rudisha tupu; +} + +// Define the nodes of the graph +fanya nodeA = { "x": 0, "y": 0, "neighbors": [] }; +fanya nodeB = { "x": 1, "y": 2, "neighbors": [] }; +fanya nodeC = { "x": 3, "y": 1, "neighbors": [] }; +fanya nodeD = { "x": 4, "y": 3, "neighbors": [] }; + +// Define the edges between the nodes +nodeA["neighbors"] = [nodeB]; +nodeB["neighbors"] = [nodeA, nodeC]; +nodeC["neighbors"] = [nodeB, nodeD]; +nodeD["neighbors"] = [nodeC]; + +// Call the A* function with the start and goal nodes and the heuristic and distance functions +//fanya path = aStar(nodeA, nodeC); + +andika(nodeA); \ No newline at end of file diff --git a/examples/reduce.nr b/examples/reduce.nr new file mode 100644 index 0000000..2f09d00 --- /dev/null +++ b/examples/reduce.nr @@ -0,0 +1,34 @@ +fanya reduce = unda(iterator, callback, initialValue) { + fanya accumulator = initialValue; + + kwa thamani ktk iterator { + accumulator = callback(accumulator, thamani); + } + + rudisha accumulator; +} + +fanya list = [1,2,3,4,5]; +fanya employees = [{"salary":120},{"salary":135},{"salary":140}] + +fanya sum = unda(acc,value){ + rudisha acc + value; +} + +fanya mul = unda(acc,value){ + rudisha acc * value; +} + +fanya sumSalo = unda(acc,value){ + rudisha acc + value["salary"]; +} + +fanya sumSaloWithTax = unda(acc,value){ + rudisha acc + (value["salary"] * (1-0.34)); +} + +andika(reduce(list,sum,0)) +andika(reduce(list,mul,1)) + +andika(reduce(employees,sumSalo,0)) +andika(reduce(employees,sumSaloWithTax,0)) \ No newline at end of file diff --git a/examples/sarufi.nr b/examples/sarufi.nr index 8c19e51..39c7419 100644 --- a/examples/sarufi.nr +++ b/examples/sarufi.nr @@ -1,32 +1,32 @@ tumia mtandao tumia jsoni pakeji sarufi { - andaa = unda(file) { - config = fungua(file) - configString = config.soma() - configDict = jsoni.dikodi(configString) - clientID = configDict["client_id"] - clientSecret = configDict["client_secret"] - params = {"client_id": clientID, "client_secret": clientSecret} - tokenString = mtandao.tuma(yuareli="https://api.sarufi.io/api/access_token", mwili=params) - tokenDict = jsoni.dikodi(tokenString) - @.token = tokenDict["access_token"] - @.Auth = "Bearer " + @.token - } + andaa = unda(file) { + config = fungua(file) + configString = config.soma() + configDict = jsoni.dikodi(configString) + clientID = configDict["client_id"] + clientSecret = configDict["client_secret"] + params = {"client_id": clientID, "client_secret": clientSecret} + tokenString = mtandao.tuma(yuareli="https://api.sarufi.io/api/access_token", mwili=params) + tokenDict = jsoni.dikodi(tokenString) + @.token = tokenDict["access_token"] + @.Auth = "Bearer " + @.token + } - tokenYangu = unda() { - rudisha @.token - } + tokenYangu = unda() { + rudisha @.token + } - tengenezaChatbot = unda(data) { - majibu = mtandao.tuma(yuareli="https://api.sarufi.io/chatbot", vichwa={"Authorization": @.Auth}, mwili = data) - rudisha majibu - } + tengenezaChatbot = unda(data) { + majibu = mtandao.tuma(yuareli="https://api.sarufi.io/chatbot", vichwa={"Authorization": @.Auth}, mwili = data) + rudisha majibu + } - pataChatbotZote = unda() { - majibu = mtandao.peruzi(yuareli="https://api.sarufi.io/chatbots", vichwa={"Authorization": @.Auth}) - rudisha majibu - } - } + pataChatbotZote = unda() { + majibu = mtandao.peruzi(yuareli="https://api.sarufi.io/chatbots", vichwa={"Authorization": @.Auth}) + rudisha majibu + } +} diff --git a/third_party/math/README.md b/third_party/math/README.md index 5e084e6..4f8acef 100644 --- a/third_party/math/README.md +++ b/third_party/math/README.md @@ -1,121 +1,158 @@ # Math Package - -A math pacakage written in pure Nuru by [VictorKariuki](https://github.com/VictorKariuki) + ## Explanation +This math package provides a collection of mathematical functions and constants implemented in the Nuru programming language. These functions cover a wide range of mathematical operations, including trigonometric functions, logarithmic functions, and other common mathematical operations. Below is a detailed list of all methods along with their descriptions and examples: + +1. **abs(namba)** + - Description: Calculates the absolute value of a number. + - Example: `Hisabati.abs(-42)` returns `42`. + +2. **acos(x)** + - Description: Calculates the arccosine (inverse cosine) of a number in radians. + - Example: `Hisabati.acos(0.5)` returns the arccosine of 0.5. + +3. **acosh(x)** + - Description: Calculates the inverse hyperbolic cosine of a number. + - Example: `Hisabati.acosh(2)` returns the inverse hyperbolic cosine of 2. + +4. **arcsin(x)** + - Description: Calculates the arcsine (inverse sine) of a number using a Taylor series. + - Example: `Hisabati.arcsin(0.5)` returns the arcsine of 0.5. + +5. **arsinh(x)** + - Description: Calculates the inverse hyperbolic sine of a number. + - Example: `Hisabati.arsinh(1)` returns the inverse hyperbolic sine of 1. + +6. **atan(x)** + - Description: Calculates the arctangent (inverse tangent) of a number in radians using a Taylor series. + - Example: `Hisabati.atan(1)` returns the arctangent of 1. + +7. **atan2(y, x)** + - Description: Calculates the angle in radians between the positive x-axis and the point (x, y). + - Example: `Hisabati.atan2(1, 1)` returns the angle for the point (1, 1). + +8. **atanh(x)** + - Description: Calculates the inverse hyperbolic tangent of a number. + - Example: `Hisabati.atanh(0.5)` returns the inverse hyperbolic tangent of 0.5. + +9. **cbrt(x)** + - Description: Calculates the cube root of a number. + - Example: `Hisabati.cbrt(8)` returns the cube root of 8. + +10. **ceil(x)** + - Description: Rounds up to the smallest integer greater than or equal to a given number. + - Example: `Hisabati.ceil(4.2)` returns `5`. -A detailed list of all methods and - Description and - Example: +11. **cos(x, terms)** + - Description: Calculates the cosine of a number in radians using a Taylor series. + - Example: `Hisabati.cos(1)` returns the cosine of 1. -### abs(namba) +12. **cosh(x)** + - Description: Calculates the hyperbolic cosine of a number. + - Example: `Hisabati.cosh(2)` returns the hyperbolic cosine of 2. -- Description: Calculates the absolute value of a number. -- Example: Hisabati.abs(-42) returns 42. +13. **exp(x, precision)** + - Description: Calculates the value of the mathematical constant e raised to the power of x using a Taylor series. + - Example: `Hisabati.exp(2)` returns `e^2`. -### acos(x) -- Description: Calculates the arccosine (inverse cosine) of a number in radians. -- Example: Hisabati.acos(0.5) returns the arccosine of 0.5. +14. **expm1(x)** + - Description: Calculates the value of e^x - 1 using a Taylor series. + - Example: `Hisabati.expm1(1)` returns `e - 1`. -### acosh(x) -- Description: Calculates the inverse hyperbolic cosine of a number. -- Example: Hisabati.acosh(2) returns the inverse hyperbolic cosine of 2. +15. **floor(x)** + - Description: Rounds down to the largest integer less than or equal to a given number. + - Example: `Hisabati.floor(4.9)` returns `4`. -### arcsin(x) -- Description: Calculates the arcsine (inverse sine) of a number using a Taylor series. -- Example: Hisabati.arcsin(0.5) returns the arcsine of 0.5. +16. **hypot(values)** + - Description: Calculates the Euclidean norm (square root of the sum of squares) of a list of values. + - Example: `Hisabati.hypot([3, 4])` returns `5`, which is the hypotenuse of a right triangle. -### arsinh(x) -- Description: Calculates the inverse hyperbolic sine of a number. -- Example: Hisabati.arsinh(1) returns the inverse hyperbolic sine of 1. +17. **log(x)** + - Description: Calculates the natural logarithm of a number using a Taylor series. + - Example: `Hisabati.log(2)` returns the natural logarithm of 2. -### atan(x) -- Description: Calculates the arctangent (inverse tangent) of a number in radians using a Taylor series. -- Example: Hisabati.atan(1) returns the arctangent of 1. +18. **log10(x)** + - Description: Calculates the base 10 logarithm of a number using the natural logarithm. + - Example: `Hisabati.log10(100)` returns `2`. -### atan2(y, x) -- Description: Calculates the angle in radians between the positive x-axis and the point (x, y). -- Example: Hisabati.atan2(1, 1) returns the angle for the point (1, 1). +19. **log1p(x)** + - Description: Calculates the natural logarithm of 1 + x using a Taylor series. + - Example: `Hisabati.log1p(0.5)` returns the natural logarithm of 1.5. -### atanh(x) -- Description: Calculates the inverse hyperbolic tangent of a number. -- Example: Hisabati.atanh(0.5) returns the inverse hyperbolic tangent of 0.5. +20. **log2(x)** -### cbrt(x) -- Description: Calculates the cube root of a number. -- Example: Hisabati.cbrt(8) returns the cube root of 8. -### ceil(x) -- Description: Rounds up to the smallest integer greater than or equal to a given number. -- Example: Hisabati.ceil(4.2) returns 5. + - Description: Calculates the base 2 logarithm of a number. + - Example: `Hisabati.log2(8)` returns `3`. -### cos(x, terms) -- Description: Calculates the cosine of a number in radians using a Taylor series. -- Example: Hisabati.cos(1) returns the cosine of 1. +21. **max(numbers)** + - Description: Returns the largest number from a list of numbers. + - Example: `Hisabati.max([3, 7, 2, 9])` returns `9`. -### cosh(x) -- Description: Calculates the hyperbolic cosine of a number. -- Example: Hisabati.cosh(2) returns the hyperbolic cosine of 2. +22. **min(numbers)** + - Description: Returns the smallest number from a list of numbers. + - Example: `Hisabati.min([3, 7, 2, 9])` returns `2`. -### exp(x, precision) -- Description: Calculates the value of the mathematical constant e raised to the power of x using a Taylor series. -- Example: Hisabati.exp(2) returns e^2. +23. **round(x, method)** + - Description: Rounds a number to the nearest integer using different rounding methods. + - Example: `Hisabati.round(3.6, "rpi")` rounds to the nearest integer (`4`) using "round half up." -### expm1(x) -- Description: Calculates the value of e^x - 1 using a Taylor series. -- Example: Hisabati.expm1(1) returns e - 1. +24. **sign(x)** + - Description: Returns the sign of a number: `1` for positive, `-1` for negative, and `0` for zero. + - Example: `Hisabati.sign(-7)` returns `-1`. -### floor(x) -- Description: Rounds down to the largest integer less than or equal to a given number. -- Example: Hisabati.floor(4.9) returns 4. +25. **sin(x, terms)** + - Description: Calculates the sine of a number in radians using a Taylor series. + - Example: `Hisabati.sin(0.5)` returns the sine of 0.5. -### hypot(values) -- Description: Calculates the Euclidean norm (square root of the sum of squares) of a list of values. -- Example: Hisabati.hypot([3, 4]) returns 5, which is the hypotenuse of a right triangle. +26. **sinh(x)** + - Description: Calculates the hyperbolic sine of a number. + - Example: `Hisabati.sinh(1)` returns the hyperbolic sine of 1. -### log(x) -- Description: Calculates the natural logarithm of a number using a Taylor series. -- Example: Hisabati.log(2) returns the natural logarithm of 2. +27. **sqrt(x)** + - Description: Calculates the square root of a number using the Newton-Raphson method. + - Example: `Hisabati.sqrt(16)` returns `4`. -### log10(x) -- Description: Calculates the base 10 logarithm of a number using the natural logarithm. -- Example: Hisabati.log10(100) returns 2. +28. **tangent(x)** + - Description: Calculates the tangent of a number in radians. + - Example: `Hisabati.tangent(1)` returns the tangent of 1. -### log1p(x) -- Description: Calculates the natural logarithm of 1 + x using a Taylor series. -- Example: Hisabati.log1p(0.5) returns the natural logarithm of 1.5. +29. **tanh(x)** + - Description: Calculates the hyperbolic tangent of a number. + - Example: `Hisabati.tanh(0.5)` returns the hyperbolic tangent of 0.5. -### log2(x) -- Description: Calculates the base 2 logarithm of a number. -- Example: Hisabati.log2(8) returns 3. +30. **isNegativeZero(num)** + - Description: Checks if a number is negative zero (0 with a negative sign). + - Example: `Hisabati.isNegativeZero(-0)` returns `true`. -### max(numbers) -- Description: Returns the largest number from a list of numbers. -- Example: Hisabati.max([3, 7, 2, 9]) returns 9. +31. **factorial(n)** + - Description: Calculates the factorial of a number. + - Example: `Hisabati.factorial(5)` returns `120`. -### min(numbers) -- Description: Returns the smallest number from a list of numbers. -- Example: Hisabati.min([3, 7, 2, 9]) returns 2. +32. **pow(base, exponent)** + - Description: Calculates the power of a number. + - Example: `Hisabati.pow(2, 3)` returns `8`. -### round(x, method) -- Description: Rounds a number to the nearest integer using different rounding methods. -- Example: Hisabati.round(3.6, "rpi") rounds to the nearest integer (4) using "round half up." +33. **isNegative(num)** + - Description: Checks if a number is negative. + - Example: `Hisabati.isNegative(-5)` returns `true`. -### sign(x) -- Description: Returns the sign of a number: 1 for positive, -1 for negative, and 0 for zero. -- Example: Hisabati.sign(-7) returns -1. +34. **isInteger(num)** + - Description: Checks if a number is an integer. + - Example: `Hisabati.isInteger(42)` returns `true`. -### sin(x, terms) -- Description: Calculates the sine of a number in radians using a Taylor series. -- Example: Hisabati.sin(0.5) returns the sine of 0.5. +35. **getIntegerPart(num)** + - Description: Gets the integer part of a number. + - Example: `Hisabati.getIntegerPart(5.8)` returns `5`. -### sinh(x) -- Description: Calculates the hyperbolic sine of a number. -- Example: Hisabati.sinh(1) returns the hyperbolic sine of 1. +36. **list(first, last, interval)** + - Description: Creates a list of numbers within a specified range with a given interval. + - Example: `Hisabati.list(0, 10, 2)` returns `[0, 2, 4, 6, 8]`. -### sqrt(x) -- Description: Calculates the square root of a number using the Newton-Raphson method. -- Example: Hisabati.sqrt(16) returns 4. +37. **square(n, i, j)** + - Description: Finds the square root of a number using a method that iteratively narrows down the root. + - Example: `Hisabati.square(16)` returns `4`. -### sine(x) -- Description: Calculates the sine of a number in radians using a Taylor series. -- Example: Hisabati.sine(1.5) returns the sine of 1.5 \ No newline at end of file +Feel free to use this package for your mathematical calculations and applications. \ No newline at end of file diff --git a/third_party/math/hisabati.nr b/third_party/math/hisabati.nr index bdbd67e..2691b19 100644 --- a/third_party/math/hisabati.nr +++ b/third_party/math/hisabati.nr @@ -1,45 +1,70 @@ pakeji hisabati{ - andaa = unda() { // the andaa function is mandatory even kama its empty - // π (Pi): The ratio of the circumference of a circle to its diameter. - @.PI = 3.141592653589793; + //CONSTRUCTOR METHOD + andaa = unda() {} - // e (Euler's Number): The base of the natural logarithm and used in various mathematical contexts. - @.e = 2.718281828459045; + // Constants + // π (Pi) + PI = unda() { + rudisha 3.141592653589793; + } - // φ (Phi, Golden Ratio): A mathematical fanyaant with applications in nature, art, and architecture. - @.phi = 1.618033988749895; + // e (Euler's Number) + e = unda() { + rudisha 2.718281828459045; + } - // Natural logarithm of 10. - @.ln10 = 2.302585092994046; + // φ (Phi, Golden Ratio) + phi = unda() { + rudisha 1.618033988749895; + } - // Natural logarithm of 2. - @.ln2 = 0.6931471805599453; + // Natural logarithm of 10 + ln10 = unda() { + rudisha 2.302585092994046; + } - // Base 10 logarithm of Euler's number (e). - @.log10e = 0.4342944819032518; + // Natural logarithm of 2 + ln2 = unda() { + rudisha 0.6931471805599453; + } - // Base 2 logarithm of Euler's number (e). - @.log2e = 1.4426950408889634; + // Base 10 logarithm of Euler's number (e) + log10e = unda() { + rudisha 0.4342944819032518; + } - // √1/2 (equivalent to 1 / sqrt(2)). - @.sqrt1_2 = 0.7071067811865476; + // Base 2 logarithm of Euler's number (e) + log2e = unda() { + rudisha 1.4426950408889634; + } - // √2 (Square Root of 2): Used in geometry and mathematical proofs. - @.sqrt2 = 1.414213562373095; + // √1/2 (equivalent to 1 / sqrt(2)) + sqrt1_2 = unda() { + rudisha 0.7071067811865476; + } - // √3 (Square Root of 3): Used in geometry, trigonometry, and mathematics. - @.sqrt3 = 1.732050807568877; + // √2 (Square Root of 2) + sqrt2 = unda() { + rudisha 1.414213562373095; + } - // √5 (Square Root of 5): Used in various mathematical contexts. - @.sqrt5 = 2.236067977499790; + // √3 (Square Root of 3) + sqrt3 = unda() { + rudisha 1.732050807568877; + } - //@.EPSILON = 2.220446049250313e-16; + // √5 (Square Root of 5) + sqrt5 = unda() { + rudisha 2.236067977499790; + } + // @.EPSILON + EPSILON = unda() { + rudisha 2.220446049250313e-16; } - // Methods - + //abs(namba), calculates the absolute value of a number. abs = unda(namba){ kama(namba < 0){ rudisha - 1 * namba; @@ -48,6 +73,7 @@ pakeji hisabati{ rudisha namba; } + //acos(x), calculates the arccosine of a number. acos = unda(x) { kama(x < -1 || x > 1) { rudisha 0; @@ -72,6 +98,7 @@ pakeji hisabati{ } } + //acosh(x), calculates the inverse hyperbolic cosine of a number. acosh = unda(x) { kama(x < 1) { rudisha 0; @@ -80,7 +107,7 @@ pakeji hisabati{ rudisha @.log(x + @.sqrt(x * x - 1)); } - // method to calculate arcsin using Taylor series + //arcsin(x), calculates the arcsine of a number using the Taylor series. arcsin = unda(x) { kama(x < -1 || x > 1) { rudisha 0; @@ -91,7 +118,7 @@ pakeji hisabati{ fanya n = 0; wakati(n < maxIterations) { - fanya numerator = @.pow(-1, n) * @.pow(x, 2 * n + 1); + fanya numerator = (-1) ** n * x ** (2 * n + 1); fanya denominator = @.factorial(2 * n + 1); // You'll need to implement a factorial function result += numerator / denominator; @@ -101,6 +128,7 @@ pakeji hisabati{ rudisha result; } + //arsinh(x), calculates the inverse hyperbolic sine of a number. arsinh = unda(x) { // Calculate arsinh using the formula: arsinh(x) = ln(x + sqrt(x^2 + 1)) kama(x >= 0) { @@ -111,6 +139,7 @@ pakeji hisabati{ } } + //atan(x), calculates the arctangent of a number using the Taylor series. atan = unda(x) { kama(x == 0) { rudisha 0; @@ -145,6 +174,7 @@ pakeji hisabati{ rudisha result; } + //atanh(x), calculates the inverse hyperbolic tangent of a number. atan2 = unda(y, x) { kama(x > 0) { rudisha @.atan(y / x); @@ -161,6 +191,7 @@ pakeji hisabati{ } } + //atanh(x), calculates the inverse hyperbolic tangent of a number. atanh = unda(x) { kama(x < -1 || x > 1) { rudisha 0; @@ -168,7 +199,7 @@ pakeji hisabati{ rudisha 0.5 * @.log((1 + x) / (1 - x)); } - // Cube Root + //cbrt(x), calculates the cube root of a number. cbrt = unda(x) { kama(x >= 0) { rudisha @.root(x, 3); @@ -177,13 +208,13 @@ pakeji hisabati{ } } - // Helper method to calculate the nth root using the Newton-Raphson method + //root(x, n), calculates the nth root of a number using the Newton-Raphson method. root = unda(x, n) { fanya guess = x / 2; // Initial guess fanya tolerance = 1e-10; // Tolerance for convergence wakati(true) { - fanya nextGuess = ((n - 1) * guess + x / @.pow(guess, n - 1)) / n; + fanya nextGuess = ((n - 1) * guess + x / guess ** n - 1) / n; kama(@.abs(nextGuess - guess) < tolerance) { rudisha nextGuess; } @@ -191,7 +222,7 @@ pakeji hisabati{ } } - // Rounds up to the smallest integer greater than or equal to a given number + //ceil(x), rounds up to the smallest integer greater than or equal to a given number. ceil = unda(x) { kama(x >= 0) { kama(x % 1 == 0) { @@ -203,6 +234,7 @@ pakeji hisabati{ } } + //cos(x), calculates the cosine of an angle in radians using the Taylor series. cos = unda(x, terms = 10) { // Initialize the result fanya n = 0; @@ -216,7 +248,7 @@ pakeji hisabati{ } sivyo { numerator = -x ** (2 * n + 1); } - fanya denominator = factorial(2 * n); + fanya denominator = @.factorial(2 * n); // Add the nth term to the result result += numerator / denominator; @@ -227,12 +259,14 @@ pakeji hisabati{ rudisha result; } + //cosh(x), calculates the hyperbolic cosine of a number. cosh = unda(x) { fanya eToX = @.exp(x); fanya eToMinusX = @.exp(-x); rudisha(eToX + eToMinusX) / 2; } + //exp(x), calculates the value of Euler's number raised to the power of a given number. exp = unda(x, precision = 15) { fanya result = 1; fanya term = 1; @@ -248,6 +282,7 @@ pakeji hisabati{ rudisha result; } + //expm1(x), calculates the value of Euler's number raised to the power of a given number minus 1. expm1 = unda(x) { fanya epsilon = 1e-15; // A small value to improve accuracy fanya result = 0; @@ -263,6 +298,7 @@ pakeji hisabati{ rudisha result; } + //floor(x), rounds down to the largest integer less than or equal to a given number. floor = unda(x) { kama(x >= 0) { rudisha x - (x % 1); @@ -271,6 +307,7 @@ pakeji hisabati{ } } + //hypot(values), calculates the square root of the sum of squares of the given values. hypot = unda(values) { // Calculate the sum of squares of the values fanya exp = unda(acc, value){ @@ -285,6 +322,7 @@ pakeji hisabati{ rudisha result; } + //log(x), calculates the natural logarithm of a number. log = unda(x) { kama(x <= 0) { rudisha 0; @@ -295,7 +333,7 @@ pakeji hisabati{ fanya i = 1; wakati(i <= n) { - approx += (1 / i) * @.pow((x - 1) / (x + 1), 2 * i - 1); + approx += (1 / i) * ((x - 1) / (x + 1)) ** (2 * i - 1); i++; } @@ -303,7 +341,7 @@ pakeji hisabati{ rudisha 2 * approx; } - // method to calculate the base 10 logarithm + //log10(x), calculates the base 10 logarithm of a number. log10 = unda(x) { kama(x <= 0) { rudisha 0; @@ -313,6 +351,7 @@ pakeji hisabati{ rudisha this.log(x) / this.log(10); } + //log1p(x), calculates the natural logarithm of 1 plus the given number. log1p = unda(x) { kama(x < -1) { rudisha 0; @@ -344,6 +383,7 @@ pakeji hisabati{ rudisha result; } + //log2(x), calculates the base 2 logarithm of a number. log2 = unda(x) { kama(x <= 0) { rudisha 0; @@ -360,6 +400,7 @@ pakeji hisabati{ rudisha result; } + //max(numbers), finds the maximum value in a list of numbers. max = unda(numbers) { // Initialize a variable to store the largest number fanya largest = -Infinity; @@ -375,6 +416,7 @@ pakeji hisabati{ rudisha largest; } + //min(numbers), finds the minimum value in a list of numbers. min = unda(numbers) { kama(numbers.length == 0) { rudisha Infinity; @@ -393,6 +435,7 @@ pakeji hisabati{ rudisha minVal; } + //round(x, method), rounds a number to the nearest integer using the specified method. round = unda(x, method = "ri") { kama(method == "rpi") { rudisha floor(x + 0.5); @@ -409,6 +452,7 @@ pakeji hisabati{ } } + //sign(x), determines the sign of a number. sign = unda(x) { kama(x == 0 || x == -0) { rudisha x; @@ -419,6 +463,7 @@ pakeji hisabati{ } } + //sin(x), calculates the sine of an angle in radians using the Taylor series. sin = unda(x, terms = 10) { // Initialize the result fanya n = 0; @@ -444,6 +489,7 @@ pakeji hisabati{ rudisha result; } + //sinh(x), calculates the hyperbolic sine of a number. sinh = unda(x) { // sinh(x) = (e^x - e^(-x)) / 2 fanya eToX = @.exp(x); @@ -451,6 +497,7 @@ pakeji hisabati{ rudisha(eToX - eToMinusX) / 2; } + //sqrt(x), calculates the square root of a number. sqrt = unda(x) { kama(x < 0) { rudisha 0; @@ -472,23 +519,8 @@ pakeji hisabati{ } } - sine = unda(x) { - fanya tolerance = @.e-15; // Tolerance for convergence - fanya result = x; - fanya term = x; - fanya n = 1; - - wakati(@.abs(term) > tolerance) { - term *= (-x * x) / ((2 * n + 1) * (2 * n)); - result += term; - n++; - } - - rudisha result; - } - - // Calculate the tangent of a number in radians - tangent = unda(x) { + //tan(x), calculates the tangent of an angle in radians. + tan = unda(x) { fanya sineX = @.sine(x); fanya cosineX = @.sqrt(1 - sineX * sineX); @@ -499,6 +531,7 @@ pakeji hisabati{ rudisha sineX / cosineX; } + //tanh(x), calculates the hyperbolic tangent of a number. tanh = unda(x) { kama(x == Infinity) { rudisha 1; @@ -512,11 +545,7 @@ pakeji hisabati{ } // utility methods - // Function to check kama a number is negative zero - isNegativeZero = unda(num) { - rudisha num == 0 && 1 / num == -Infinity; - } - // Helper function to calculate the factorial of a number + //factorial(n), calculates the factorial of a number. factorial = unda(n) { kama(n == 0){ rudisha 1; @@ -533,27 +562,17 @@ pakeji hisabati{ } - // Helper function to calculate pow using Taylor series - pow = unda(base, exponent) { - fanya result = 1; - fanya i = 0; - wakati(i < exponent) { - result *= base; - i++; - } - rudisha result; - } - - // Helper function to check kama a number is negative + //isNegative(num), checks if a number is negative. isNegative = unda(num) { rudisha num < 0; } + //isInteger(num), checks if a number is an integer. isInteger = unda(num) { rudisha num == @.floor(num); } - // method to get the integer part of a number + //getIntegerPart(num), gets the integer part of a number. getIntegerPart = unda(num) { // Handle negative numbers separately kama(@.isNegative(num)) { @@ -565,7 +584,8 @@ pakeji hisabati{ } } - // create a list of numbers + //Arrray Methods + //list(first, last, interval), creates a list of numbers with the specified interval between theM. list = unda(first, last, interval){ fanya list = [first]; fanya i = first + interval; @@ -576,33 +596,7 @@ pakeji hisabati{ rudisha list; } - // Maths functions - // square a number - square = unda(n, i, j){ - fanya kati = (i + j) / 2; - fanya mul = kati * kati; - fanya abs_diff = abs(mul - n); - - kama(mul == n || abs_diff < 0.00001){ - rudisha kati; - }au kama(mul < n){ - rudisha square(n, kati, j); - }au{ - rudisha square(n, i, kati); - } - } - - // find the square root of a number - sqrt = unda(namba){ - kwa i ktk list(0, namba, 1) { - kama((i * i) == namba){ - rudisha i; - }au kama((i * i) > namba){ - rudisha square(namba, i - 1, i); - } - } - } - + //reduce(iterator, callback, initialValue), reduces the elements of an array to a single value using a specified callback function. reduce = unda(iterator, callback, initialValue) { fanya accumulator = initialValue;