Understanding Mathematical Symbols with Code

Ian Rowan Ian Rowan
March 12, 2020 AI & Machine Learning

Summations, factorials, matrices and more are simple when we look at them with our favorite languages

Understanding Mathematical Symbols with Code

Source

For anyone interested in pursuing a career or research in Machine Learning and Data Science, the day will come when it is time to move beyond the python libraries to follow curiosities into the math behind it all. This will typically take you to the vast, open collection of papers detailing how it all works. The deeper you go to understand the core mathematics, the closer you may come to the flash of insight to create a new method. Everything seems to be all right on the first paper until you come across something like this:

Understanding Mathematical Symbols with Code

For anyone who has studied math for years or worked at the math level of Machine Learning, such an equation can be carefully parsed into meaning and code. For many others though, this may look like hieroglyphics. The truth is that it almost seems like the ancient math leaders chose the most interesting looking symbols to describe rather intuitive methods. The result: Equations and variables that look much more complex than they really are.

I’ve found that code can be used for more than writing programs, but also a globally agreed-upon language to explain the complex. When I was learning the math behind everything Data Science, I always found that the best way to gain a universal understanding of math was to write code snippets to describe equations. Eventually, these symbols become understood to a point that they can almost be read as text in a typical paper. In this article, I hope to share some examples of how simple math can be when described with code!

Summation and Product

Understanding Mathematical Symbols with Code

The summation symbol is one of the most useful and commonly used symbols in iterative mathematics. Despite its complex design, the implementation is rather simple, yet incredibly useful.

x = [1, 2, 3, 4, 5, 6]
result = 0for i in range(6):
result += x[i]Output of print(result) -> 21

As seen above, all that this symbol represents is a for loop in the range of the number on top, starting from the number on the bottom. The variable set on the bottom becomes the index variable and any result per loop is added to an overall value. Less commonly, the following may be used:

Understanding Mathematical Symbols with Code

Typically called the Product Operator, this symbol functions in the same manner, but instead of adding each result they will be multiplied.

x = [1, 2, 3, 4, 5, 1]
result = 1for i in range(6):
result *= x[i]Output of print(result) -> 120

Factorial

Factorial is the “!” which exists on almost any calculator. To many, this one may be a bit more obvious, but it is still worth it to write some code to understand the mechanics.

5! would be represented as:

result = 1
for i in range(1,6):
result *= i
Output of print(result) -> 120

Conditional Brackets

Conditional Brackets

Conditional brackets are used to divert the flow of an equation based on a set of conditions. For coders, this is simply the common “if” statement. The above conditional can be represented as:

i = 3
y = [-2, 3, 4, 1]
result = 0if i in y:
result = sum(y)
elif i > 0:
result = 1
else:
result = 0print(result) -> 6

As seen above, the right notation of each row of the bracket dictates what each path should execute. I threw the extra “contains” symbol into each condition as well to add more insight. As seen above, we checked if the i value was in the y list. Recognizing that it was, we returned the sum of the array. If the i value had not been in the array we would have returned 0 or 1 based on the value.

Point Wise and Cartesian Matrix Multiplication

Finally, I wanted to quickly cover the operations which are typically done for any Data Scientist by their favorite language library — matrix multiplication. The easiest form to understand is the point wise operation. This is simply written as:

Point Wise and Cartesian Matrix Multiplication

Note the first requirement is that each matrix must have the same shape(ie # rows= & #Columns=)

The code for this looks as follows:

y = [[2,1],[4,3]]
z = [[1,2],[3,4]]
x = [[0,0],[0,0]]for i in range(len(y)):
for j in range(len(y[0])):
x[i][j] = y[i][j] * z[i][j]print(x) -> [[2, 2], [12, 12]]  

Lastly let’s take a look at a typical matrix multiplication process, most commonly used in Machine Learning. In complex terms, this operation finds the dot product of each primary row with each secondary column. The main take away from this is the the following requirement: assume [#rows, #columns] →matrices i x j requires #columns(i) == #rows(j) → with a final product of shape [#rows(i), #columns(j)]

This may seem confusing and my best suggestion would be to take a look at google images for some great visualizations of these requirements.

Understanding Mathematical Symbols with Code

The code for this equation looks as follows(using numpy dot method):

y = [[1,2],[3,4]]
z = [[2], [1]]
# x has shape [2, 1]
x = [[0], [0]]for i in range(len(y))
for j in range(len(z):
x[i][j] = np.dot(y[i], z[:, j])
print(x) -> [[4],
[10]]

This was just a few examples, but the understanding of this simple code can allow any programmer to take on the initially ominous world of mathematics. Of course, these methods can all be consolidated for efficiency and usually have a library method readily available. The point of writing these in simple code is to see how much sense they make when written out in the form of their true operations.

 Sources

[1]https://commons.wikimedia.org/wiki/File:Pure-mathematics-formulæ-blackboard.jpg

  • Experfy Insights

    Top articles, research, podcasts, webinars and more delivered to you monthly.

  • Ian Rowan

    Tags
    Machine Learning
    Leave a Comment
    Next Post
    The Most Useful ML Tools 2020

    The Most Useful ML Tools 2020

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    More in AI & Machine Learning
    AI & Machine Learning,Future of Work
    AI’s Role in the Future of Work

    Artificial intelligence is shaping the future of work around the world in virtually every field. The role AI will play in employment in the years ahead is dynamic and collaborative. Rather than eliminating jobs altogether, AI will augment the capabilities and resources of employees and businesses, allowing them to do more with less. In more

    5 MINUTES READ Continue Reading »
    AI & Machine Learning
    How Can AI Help Improve Legal Services Delivery?

    Everybody is discussing Artificial Intelligence (AI) and machine learning, and some legal professionals are already leveraging these technological capabilities.  AI is not the future expectation; it is the present reality.  Aside from law, AI is widely used in various fields such as transportation and manufacturing, education, employment, defense, health care, business intelligence, robotics, and so

    5 MINUTES READ Continue Reading »
    AI & Machine Learning
    5 AI Applications Changing the Energy Industry

    The energy industry faces some significant challenges, but AI applications could help. Increasing demand, population expansion, and climate change necessitate creative solutions that could fundamentally alter how businesses generate and utilize electricity. Industry researchers looking for ways to solve these problems have turned to data and new data-processing technology. Artificial intelligence, in particular — and

    3 MINUTES READ Continue Reading »

    About Us

    Incubated in Harvard Innovation Lab, Experfy specializes in pipelining and deploying the world's best AI and engineering talent at breakneck speed, with exceptional focus on quality and compliance. Enterprises and governments also leverage our award-winning SaaS platform to build their own customized future of work solutions such as talent clouds.

    Join Us At

    Contact Us

    1700 West Park Drive, Suite 190
    Westborough, MA 01581

    Email: support@experfy.com

    Toll Free: (844) EXPERFY or
    (844) 397-3739

    © 2023, Experfy Inc. All rights reserved.