{"id":82,"date":"2021-11-28T20:50:07","date_gmt":"2021-11-28T20:50:07","guid":{"rendered":"https:\/\/zerotobyte.com\/?p=82"},"modified":"2021-12-20T14:40:42","modified_gmt":"2021-12-20T14:40:42","slug":"how-to-calculate-python-square-root","status":"publish","type":"post","link":"https:\/\/zerotobyte.com\/how-to-calculate-python-square-root\/","title":{"rendered":"How to Calculate Python Square Root?"},"content":{"rendered":"\n
The square root<\/strong> of a given number x is a number that multiplied by itself gives x. Mathematically, the roots of x2<\/sup> are x and -x.<\/p>\n\n\n\n In this article, the focus will be on the temporal analysis of the square root in python using a variety of methods.<\/p>\n\n\n\n Time analysis is calculated using decorator @time_decorator. All values are displayed in nanoseconds (ns).<\/p>\n\n\n\n Every positive number has 2 square roots<\/strong> (positive and negative). In this blog post, we consider both positive and negative integers and their result.<\/p>\n\n\n\n Why are we even talking about roots? The roots have always been computationally difficult to compute (in time). With newer and more powerful CPUs this problem has been reduced to a minimum. <\/p>\n\n\n\n There are multiple methods of computing these roots. <\/p>\n\n\n\n We will try to explore the differences between each and give some understandable summary using python. <\/p>\n\n\n\n So what is a square root in python? The square root in python is a number that multiplied by itself gives a squared number.<\/strong><\/p>\n\n\n\n In this blog post, we will consider 5 methods<\/strong> and they are summarized here, <\/p>\n\n\n\n Each of these methods has some advantages depending on the use case.<\/p>\n\n\n\n For basic usage consider math library.<\/p>\n\n\n\n The pros and cons of every method are described here.<\/p>\n\n\n\n Math module is one of the most used libraries within python. <\/strong>By default, a variety of methods is defined for more or less all the mathematical functions used. One of those methods is sqrt().<\/p>\n\n\n\n According to the documentation, Example 1.<\/strong><\/p>\n\n\n\n As you can see Let\u2019s look at the following python square root example.<\/p>\n\n\n\n Example 2.<\/strong><\/p>\n\n\n\n Math module does not support rooting negative numbers<\/strong> and ValueError occurs. Rooting zero works as intended as it returns 0.<\/p>\n\n\n\n Math library documentation<\/a><\/p>\n\n\n\n As already mentioned python square roots time analysis will be displayed in nanoseconds (ns) to be as accurate as possible.<\/p>\n\n\n\n Example 1.<\/strong><\/p>\n\n\n\n Execution time can and must vary depending on the computer’s specifications.<\/strong> In my example, during one execution, the execution time lasted 2669 ns. <\/p>\n\n\n\n The average execution time based on 100 executions is 2354 ns.<\/strong><\/p>\n\n\n\n Cmath is an extension of the standard math module<\/strong> that supports working with complex numbers.<\/p>\n\n\n\n <\/p>\n\n\n\n Example 1.<\/strong><\/p>\n\n\n\n Let\u2019s look at the following python square root example.<\/p>\n\n\n\n Example 2.<\/strong><\/p>\n\n\n\n Here, unlike the standard math module, there is no ValueError.<\/p>\n\n\n\n Cmath library documentation<\/a><\/p>\n\n\n\n Example 1.<\/strong><\/p>\n\n\n\n The average execution time based on 100 executions is 14085 ns.<\/strong><\/p>\n\n\n\n The Double star operator (**) is one of Python’s built-in arithmetic operators<\/strong>. In the official python documentation also referred to as the power operator. It works similarly to the pow function that we will cover in the next section.<\/p>\n\n\n\n Example 1.<\/strong><\/p>\n\n\n\n If we root a positive number we get a float as a result.<\/strong><\/p>\n\n\n\n Let’s look at what happens when we root a negative number:<\/p>\n\n\n\n Example 2.<\/strong><\/p>\n\n\n\n Here we get a complex number<\/strong> as with the cmath module but a slightly different result, interesting? \ud83d\ude42<\/p>\n\n\n\n Example 1.<\/strong><\/p>\n\n\n\n The average execution time based on 100 executions is 11069 ns.<\/strong><\/p>\n\n\n\n Numpy is a popular library for mathematical calculations in python.<\/strong> Numpy offers all kinds of data manipulation tools and built-in functions.<\/p>\n\n\n\n We will focus on Example 1.<\/strong><\/p>\n\n\n\n Let\u2019s look at the following python square root example in NumPy.<\/p>\n\n\n\n Example 2.<\/strong><\/p>\n\n\n\n This method does not support negative numbers but does not throw an error but a warning.<\/strong> The return value is, therefore, nan (not a number) of class float64.<\/p>\n\n\n\n NumPy documentation<\/a><\/p>\n\n\n\n Example 1.<\/strong><\/p>\n\n\n\n The average execution time based on 100 executions is 6099 ns.<\/strong><\/p>\n\n\n\n As we have already mentioned the pow function works similarly to the ** operator<\/strong>, so let\u2019s compare results.<\/p>\n\n\n\n Example 1.<\/strong><\/p>\n\n\n\n As you can see Let\u2019s look at the following python square root example.<\/p>\n\n\n\n Example 2.<\/strong><\/p>\n\n\n\n Same result as double star (**) operator \ud83d\ude42<\/p>\n\n\n\n We expect similar time analysis results but let\u2019s check just in case.<\/p>\n\n\n\n Pow function documentation<\/a><\/p>\n\n\n\n Example 1.<\/strong><\/p>\n\n\n\n The average execution time based on 100 executions is 10883 ns.<\/strong><\/p>\n\n\n\n We see that the temporal analysis also showed that the pow and ** operators are the same things.<\/p>\n\n\n\n In this section, I will make a comparison of all 5 methods of calculating python square root.<\/p>\n\n\n\nSquare roots using python<\/h2>\n\n\n\n
math.sqrt(x)<\/code><\/li>
cmath.sqrt(x)<\/code><\/li>
x**0.5<\/code><\/li>
numpy.sqrt(x)<\/code><\/li>
pow(x, 0.5)<\/code><\/li><\/ul>\n\n\n\n
Python square root using math library<\/h2>\n\n\n\n
sqrt(x)<\/code> returns the rooted number x<\/strong>.<\/p>\n\n\n\n
import math\n\t\nx = math.sqrt(4)\nprint(f\"The square root of 4 = {x} and type of x is {type(x).__name__}\")\n<\/pre>\n\n\n\n
Output: The square root of 4 = 2.0 and type of x is float<\/pre>\n\n\n\n
math.sqrt(x)<\/code> returns the float by default.<\/strong><\/p>\n\n\n\n
import math\n\t\nx = math.sqrt(-4)\nprint(f\"The square root of -4 = {x} and type of x is {type(x).__name__}\")\n<\/pre>\n\n\n\n
Output: ValueError: math domain error<\/pre>\n\n\n\n
Time analysis of math.sqrt function<\/h3>\n\n\n\n
@time_decorator\ndef math_lib_time_example(number):\n\treturn math.sqrt(number)\n\nprint(f\"The square root of 4 = {math_lib_time_example(4)}\")\n<\/pre>\n\n\n\n
Output:\nTotal execution time: 2669 ns\nThe square root of 4 = 2.0<\/pre>\n\n\n\n
Python square root using cmath library<\/h2>\n\n\n\n
import cmath\n\t\nx = cmath.sqrt(4)\nprint(f\"The square root of 4 = {x} and type of x is {type(x).__name__}\")\n<\/pre>\n\n\n\n
Output: The square root of 4 = (2+0j) and type of x is complex<\/pre>\n\n\n\n
cmath.sqrt(x)<\/code> returns complex by default.<\/strong><\/p>\n\n\n\n
import cmath\n\t\nx = cmath.sqrt(-4)\nprint(f\"The square root of -4 = {x} and type of x is {type(x).__name__}\")\n<\/pre>\n\n\n\n
Output: The square root of -4 = 2j and type of x is complex<\/pre>\n\n\n\n
Time analysis of cmath.sqrt function<\/h3>\n\n\n\n
@time_decorator\ndef cmath_lib_time_example(number):\n\treturn cmath.sqrt(number)\n\nprint(f\"The square root of 4 = {cmath_lib_time_example(4)}\u201d)\n<\/pre>\n\n\n\n
Output: \nTotal execution time: 16665 ns\nThe square root of 4 = (2+0j)<\/pre>\n\n\n\n
Python square root using ** operator<\/h2>\n\n\n\n
x = 4 ** 0.5\nprint(f\"The square root of 4 = {x} and type of x is {type(x).__name__}\")\n<\/pre>\n\n\n\n
Output: The square root of 4 = 2.0 and type of x is float<\/pre>\n\n\n\n
x =(-4)**0.5 #parentheses are important because ** operator has an advantage over multiplication\nprint(f\"The square root of -4 = {x} and type of x is {type(x).__name__}\")\n<\/pre>\n\n\n\n
Output: The square root of -4 = (1.2246467991473532e-16+2j) and type of x is complex<\/pre>\n\n\n\n
Time analysis of ** operator<\/h3>\n\n\n\n
@time_decorator\ndef double_star_time_example(number):\n\treturn number**0.5\n\nprint(f\"The square root of 4 = {double_star_time_example(4)})\n<\/pre>\n\n\n\n
Output: \nTotal execution time: 10332 ns\nThe square root of 4 = 2.0<\/pre>\n\n\n\n
Python square root using NumPy<\/h2>\n\n\n\n
numpy.sqrt(x)<\/code>.<\/p>\n\n\n\n
import numpy\n\t\nx = numpy.sqrt(4)\nprint(f\"The square root of 4 = {x} and type of x is {type(x).__name__}\")\n<\/pre>\n\n\n\n
Output: The square root of 4 = 2.0 and type of x is float64<\/pre>\n\n\n\n
numpy.sqrt(x)<\/code> returns the float64 class by default.<\/strong> Float64 is a NumPy class built into the NumPy library.<\/p>\n\n\n\n
import numpy\n\t\nx = numpy.sqrt(-4)\nprint(f\"The square root of -4 = {x} and type of x is {type(x).__name__}\")\n<\/pre>\n\n\n\n
Output: \nRuntimeWarning: invalid value encountered in sqrt x = numpy.sqrt(-4)\nThe square root of -4 = nan and type of x is float64<\/pre>\n\n\n\n
Time analysis of numpy.sqrt function<\/h3>\n\n\n\n
@time_decorator\ndef numpy_time_example(number):\n\treturn numpy.sqrt(number)\n\nprint(f\"The square root of 4 = {numpy_time_example(4)}\")\n<\/pre>\n\n\n\n
Output:\nTotal execution time: 5912 ns\nThe square root of 4 = 2.0<\/pre>\n\n\n\n
Python square root using pow<\/h2>\n\n\n\n
x = pow(4, 0.5)\nprint(f\"The square root of 4 = {x} and type of x is {type(x).__name__}\")\n<\/pre>\n\n\n\n
Output: The square root of 4 = 2.0 and type of x is float<\/pre>\n\n\n\n
pow(x)<\/code> returns float by default.<\/strong><\/p>\n\n\n\n
x = pow(-4, 0.5)\nprint(f\"The square root of -4 = {x} and type of x is {type(x).__name__}\")\n<\/pre>\n\n\n\n
Output: The square root of -4 = (1.2246467991473532e-16+2j) and type of x is complex<\/pre>\n\n\n\n
Time analysis of pow<\/h3>\n\n\n\n
@time_decorator\ndef pow_time_example(number):\n\treturn pow(number, 0.5)\n\t\nprint(f\"The square root of 4 = {pow_time_example(4)}\u201d)\n<\/pre>\n\n\n\n
Output: \nTotal execution time: 11561 ns\nThe square root of 4 = 2.0<\/pre>\n\n\n\n
Summary<\/h2>\n\n\n\n