Try mathematical formulas using Σ with python

table of contents

-Background -[What is Σ](http://qiita.com/weedslayer/items/25c38c928ad59ba61071#What is Σ) -[What you want to do](http://qiita.com/weedslayer/items/25c38c928ad59ba61071 #What you want to do) -[Check operation with Python](http://qiita.com/weedslayer/items/25c38c928ad59ba61071#Check operation with Python) -[Lighten the example](http://qiita.com/weedslayer/items/25c38c928ad59ba61071 #Lighten the example) -[Extra edition ~ what if ~](http://qiita.com/weedslayer/items/25c38c928ad59ba61071 # Extra edition ~ what if ~)

Background

  1. I come across Σ (sigma) in class
  2. It's been a long time and I can think of how easy it can be expressed using Python.
  3. Gently look
  4. Discover [Possible Resources]
  5. Give it a try

What is Σ

In a word, it is expressed as sum or (mainly) the sum of a sequence.

sigma.jpg

See below for detailed usage:

What was the meaning of Σ? [Sequence 9] Sigma 1 What is Sigma? Explain the meaning and calculation of Sigma [High 2 Mathematics / Number B] Sum of sequences ~ Definition and calculation method of sigma Σ: Visual mathematics (math B: sequence

Thing you want to do

Refer to the source, 544x215xsigma.png.pagespeed.ic.f1Iqf-52cp.png

I managed to get it to work with python.

Confirmed operation with Python

Below is the original code

sigma.py


def f(x):
    return 2*x + 1

def g(x):
    return (x-1) * (x-3)

def sigma(func, frm, to:
    result = 0;
    for i in range(frm, to+1):
        result += func(i)
    return result

sigma(f, 2, 4) #21
sigma(g, 1, 3) #-1

See here for the detailed flow.

Make this work with Python 3.x

sigma2.py


def f(x):
	return 

def sigma(func, frm, to):
	result = 0;
	for i in range(frm, to+1):
		result += func(i)
	print(result)

sigma(f, 1, 10) #21

Lighten the example

Example: Find the sum of the following equations.

math.png

this

math.png --- #1

When

math copy.png --- #2 (これが上の画像で言うk番目のa)

Divide into.

First, create a function that gives the answer of # 2.

def f(n):
    return 3 * n - 2

And the function that gives the answer of # 1

def sigma(func, frm, to):
	result = 0; #Answer saucer
	for i in range(frm, to+1):
		result += func(i) 
		#Call the function here. By the way, here i= x
	print(result)

The completed form looks like this

ef f(n):
	return 3 * n - 2  

def sigma(func, frm, to):
	result = 0; #Answer saucer
	for i in range(frm, to+1):
		result += func(i) 
		#Call the function here. By the way, here i= x
	print(result)

sigma(f, 1, 4) #22

Extra edition ~ what if ~

If you come across an expression that doesn't require you to create an expression for f (x)

  1. Delete the argument func of the sigma function
  2. From result + = func (i) to result + = i
  3. This works fine. .. Should be
def sigma2(frm, to):
	result = 0;
	for i in range(frm, to+1):
		result += i
	print(result)

sigma2(1,10) #55

Recommended Posts

Try mathematical formulas using Σ with python
Try using Python with Google Cloud Functions
Try scraping with Python.
Try using Tweepy [Python2.7]
Try using PythonTex with Texpad.
[S3] CRUD with S3 using Python [Python]
[Python] Try using Tkinter's canvas
Using Quaternion with Python ~ numpy-quaternion ~
Try using matplotlib with PyCharm
Try Python output with Haxe 3.2
Try using Kubernetes Client -Python-
[Python] Using OpenCV with Python (Basic)
Try projective transformation of images using OpenCV with Python
Understanding with mathematical formulas and Python LiNGAM (ICA version)
Try running Python with Try Jupyter
Try face recognition with Python
Try using folium with anaconda
Using OpenCV with Python @Mac
Send using Python with Gmail
Getting Started with python3 # 3 Try Advanced Computations Using Import Statements
Try encryption / decryption using OpenSSL key with Python3 pow function
Complement python with emacs using company-jedi
Harmonic mean with Python Harmonic mean (using SciPy)
Try scraping with Python + Beautiful Soup
[Python] Using OpenCV with Python (Image Filtering)
Using Rstan from Python with PypeR
[Python] Using OpenCV with Python (Image transformation)
Try to operate Facebook with Python
[Python] Using OpenCV with Python (Edge Detection)
Try singular value decomposition with Python
Try using Pleasant's API (python / FastAPI)
Try using LevelDB in Python (plyvel)
Try face recognition with python + OpenCV
Try using Python argparse's action API
Notes on using rstrip with python.
Try using the Python Cmd module
Try frequency control simulation with Python
Try using Python's networkx with AtCoder
Try using Leap Motion in Python
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
Try using Amazon DynamoDB from Python
Try python
[SAM] Try using RDS Proxy with Lambda (Python) [user/pass, IAM authentication]
[AWS] Using ini files with Lambda [Python]
Try to reproduce color film with Python
Try logging in to qiita with Python
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Behind the flyer: Using Docker with Python
Try working with binary data in Python
Using Python and MeCab with Azure Databricks
Try using the camera with Python's OpenCV
Socket communication using socketserver with python now
Try using Dialogflow (formerly API.AI) Python SDK #dialogflow
Try using Dropbox API v2 with Go
Try using Junos On-box Python # 2 Commit Script
Check stock prices with slackbot using python
Working with OpenStack using the Python SDK
Tips for using python + caffe with TSUBAME
Try to operate Excel using Python (Xlwings)