前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Represent code in math equations

Represent code in math equations

作者头像
绿巨人
发布2018-05-16 17:50:02
6910
发布2018-05-16 17:50:02
举报
文章被收录于专栏:绿巨人专栏

Introduce

The article shows a way to use math equations to represent code's logical.

Key ideas

logical first

Get readers to see the logical first, so prefer to keep a 'where' section to describe variables separately.

Pure function conception

I am thinking, in a function, whether we can just use

  • Only has a 'if/else' statement
  • Only has a 'switch/case' statement
  • Only has a 'for' statement
  • Only has a 'foreach' statement
  • Only has a 'while' statement
  • Only has statements without 'if/else', 'switch/case', 'for', 'foreach' or 'while'.

Decision Structures

  • Decision structure - simple result = \begin{cases} statement_1, & {condition}_1 \\ statement_2, & {condition}_2 \\ statement_3 & \text{otherwise} \end{cases}
  • Decision structure (if ... then) - if, else result = \begin{cases} statement_1, & \text{if}\ {condition}_1 \\ statement_2, & \text{if}\ {condition}_2 \\ statement_3 & \text{otherwise} \end{cases}
  • Decision structure (if ... then) - if, else with multiple lines \text{if (constant-expression)} \\ \{ \\ \qquad statement_1 \\ \qquad statement_2 \\ \}
  • Decision structure(selection) - switch, case, default, goto, break result = (switch : {expression}) \begin{cases} statement_1, & \text{case}\ {constant-expression}_1 \\ statement_2, & \text{case}\ {constant-expression}_2 \\ statement_3 & \text{otherwise} \end{cases}
  • Loop structure (conditional) - while, continue First check the condition to determine if enter the loop. {while}_\text{condition} \text{statement}
  • Loop structure (conditional) - do, while, continue First execute the statement, then check the condition to determine if enter the next loop. {while}^\text{condition} \text{statement}
  • Loop structure (iteration) - for {for}_\text{i = 1}^\text{ i < n} \text{statement}
  • Loop structure (iteration) - for with step {for}_\text{i = 1}^\text{ i < n; i += 2} \text{statement}
  • Loop structure (iteration) - foreach {foreach}_\text{item}^\text{items} \text{statement}

Keywords

  • break \bigotimes
  • continue \bigodot

function

\text{(variable_1 [, ..., variable_n]) function_name(parameter_1 [, ..., parameter_k])} = \\ [\{] \\ \qquad statement_1 \\ \qquad statement_2 \\ \qquad ... \\ \qquad statement_n \\ where \\ \qquad variable_1 = ... \\ \qquad ... \\ \qquad variable_m = ... \\ [\}]

function in one body

\text{(variable_1 [, ..., variable_n]) function_name(parameter_1 [, ..., parameter_l])} = \\ \qquad \begin{cases} statement_1 \\ statement_2 \\ where \\ \qquad variable_1 = ... \\ \qquad ... \\ \qquad variable_m = ... \\ \end{cases} \\

class

\text{class class_name[(inherited class name)]} = \\ [\{] \\ [where] \\ \qquad field_1 = ... \\ \qquad ... \\ \qquad field_m = ... \\ [functions]\\ \qquad function_1 \\ \qquad function_2 \\ \qquad ... \\ \qquad function_n \\ [\}]

Comment - single line

\text{ # input some comments} \\ \text{ : input some comments}

Comment - multiple lines

''' \\ \text{ this is} \\ \text{ multiple lines comments} \\ '''

Comment - multiple lines 2

""" \\ \text{ this is} \\ \text{ multiple lines comments} \\ """

Sample

\text{class firstclass} = \\ \qquad field1 = 1 \\ \qquad field2 = true \\ \qquad \\ \qquad func1(param1, y) \\ \qquad \{ \\ \qquad \qquad var1,\ var2 = func2(1,\ 2) = \\ \qquad \qquad func3(var1) \\ \qquad \qquad y^{(mean)} = f4(y) \\ \qquad where \\ \qquad \qquad var1 \text{ # return value 1}\\ \qquad \qquad var2 \text{ # return value 2}\\ \qquad \qquad y \text{ : result data of training data.} \\ \qquad \qquad y^{(mean)} \text{ : the arithmetic mean along the y.} \\ \qquad \} \\ \qquad \\ \qquad (result1,\ result2)\ func2(param1,\ param2) = \\ \qquad \{ \\ \qquad \qquad result1 = \begin{cases} 1, & \text{param1 > 0} \\ -1 & \text{otherwise} \end{cases} \\ \qquad \qquad var2 = 100 \text{ # it is a variable defined in body. }\\ \qquad \qquad result2 = var1 + var2 + param2 \\ \qquad where \\ \qquad \qquad result1 \text{ # return value 1} \\ \qquad \qquad result2 = 0 \text{ # return value 2} \\ \qquad \qquad var1 = 10 \text{ # it is a variable. } \\ \qquad \} \\ \qquad \\ \qquad func3(param1) = \\ \qquad \begin{cases} \text{# do something ...} \\ \text{# do something ...} \\ \text{# do something ...} \\ \text{# do something ...} \\ \text{# do something ...} \\ \text{# do something ...} \\ \end{cases} \\ \qquad \\ \qquad f4(y) = \frac{sum(y)}{count(y)} \\ \qquad \\ \qquad

References

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-07-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Introduce
  • Key ideas
    • logical first
      • Pure function conception
      • Decision Structures
      • Keywords
      • function
      • function in one body
      • class
      • Comment - single line
      • Comment - multiple lines
      • Comment - multiple lines 2
      • Sample
        • References
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档