美文网首页
基础Julia笔记

基础Julia笔记

作者: Yung968 | 来源:发表于2019-07-23 21:17 被阅读0次

    工作需要,要求快速学习Julia,这是在一份骨干基础知识,能够尽量实现快速上手。

    中文文档地址

    学习开始时间 2019-3-29 15:13

    变量

    定义方式和python类似

    格式为

    • 变量的名字采用小写。

    • 用下划线(_)分隔名字中的单词,但是不鼓励使用下划线, 除非在不使用下划线时名字会非常难读。

    • 类型(Type)和模块(Module)的名字使用大写字母开头,并且用大写字母 而不是用下划线分隔单词。

    • 函数(Function)和宏(Macro)的名字使用小写,不使用下划线。

    • 会对输入参数进行更改的函数要使用 ! 结尾。这些函数有时叫做 “mutating” 或 “in-place” 函数,因为它们在被调用后,不仅仅会返回一些值 还会更改输入参数的内容。

    整数和浮点数、复数和有理数

    typeof()可以用来查看数据类型。

    支持a=1.6e2这种科学计数法的定义方式。

    全局im被绑定到复数i,与其他数据类型一样可以正常操作。

    julia支持通过//整数比创建精确有理数,例如2//3

    数学运算和初等函数

    算数运算符

    与其他高级语言基本相同。加入一下三个比较特殊的:

    X \ Y 反向除法 等价于 Y / X
    x ^ y 幂操作符 xy 次幂
    x % y 取余 等价于 rem(x,y)

    位运算符

    ~X 按位取反
    x & y 按位与
    `x y` 按位或
    x ⊻ y 按位异或(逻辑异或)
    x >>> y 逻辑右移
    x >> y 算术右移
    x << y 逻辑/算术左移

    dot运算符

    和python的numpy相反,.代表按照对应位置进行操作

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="julia" cid="n63" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); --select-text-bg-color: #36284e; --select-text-font-color: #fff; font-size: 0.9rem; line-height: 1.714285em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(218, 218, 218); position: relative !important; margin-bottom: 3em; margin-left: 2em; padding-left: 1ch; padding-right: 1ch; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">[1,2,3] .^ 3
    3-element Array{Int64,1}:
    1
    8
    27</pre>

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="julia" cid="n67" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); --select-text-bg-color: #36284e; --select-text-font-color: #fff; font-size: 0.9rem; line-height: 1.714285em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(218, 218, 218); position: relative !important; margin-bottom: 3em; margin-left: 2em; padding-left: 1ch; padding-right: 1ch; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">isequal(a, b) # x y是否完全相同(数值相等)
    isfinite(x) # x是有限大的数字
    isinf(x) # x是无穷大(正负都可以)
    isnan(x) # x是否是NaN。NaN不是数字,他是未定义或者不可表示的数据类型。例如0/0例如确实数。</pre>

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="julia" cid="n82" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); --select-text-bg-color: #36284e; --select-text-font-color: #fff; font-size: 0.9rem; line-height: 1.714285em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(218, 218, 218); position: relative !important; margin-bottom: 3em; margin-left: 2em; padding-left: 1ch; padding-right: 1ch; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">julia> findfirst(isequal('x'), "xylophone") # 寻找特定字符串位置
    1</pre>

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="julia" cid="n84" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); --select-text-bg-color: #36284e; --select-text-font-color: #fff; font-size: 0.9rem; line-height: 1.714285em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(218, 218, 218); position: relative !important; margin-bottom: 3em; margin-left: 2em; padding-left: 1ch; padding-right: 1ch; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">julia> findnext(isequal('o'), "xylophone", 1)
    4

    julia> findnext(isequal('o'), "xylophone", 5)
    7

    julia> findnext(isequal('o'), "xylophone", 8)</pre>

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="julia" cid="n87" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); --select-text-bg-color: #36284e; --select-text-font-color: #fff; font-size: 0.9rem; line-height: 1.714285em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(218, 218, 218); position: relative !important; margin-bottom: 3em; margin-left: 2em; padding-left: 1ch; padding-right: 1ch; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">julia> occursin("world", "Hello, world.")
    true</pre>

    1. 对于不可变对象作为函数参数,相当于C系语言的值传递;

    2. 对于可变对象作为函数参数,相当于C系语言的引用传递。

      操作符也是函数

      <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="julia" cid="n97" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); --select-text-bg-color: #36284e; --select-text-font-color: #fff; font-size: 0.9rem; line-height: 1.714285em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(218, 218, 218); position: relative !important; margin-bottom: 3em; margin-left: 2em; padding-left: 1ch; padding-right: 1ch; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">julia> 1 + 2 + 3
      6

      julia> +(1,2,3)
      6</pre>

      <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="julia" cid="n98" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); --select-text-bg-color: #36284e; --select-text-font-color: #fff; font-size: 0.9rem; line-height: 1.714285em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(218, 218, 218); position: relative !important; margin-bottom: 3em; margin-left: 2em; padding-left: 1ch; padding-right: 1ch; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">julia> f = +;

      julia> f(1,2,3)
      6</pre>

      <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="julia" cid="n102" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); --select-text-bg-color: #36284e; --select-text-font-color: #fff; font-size: 0.9rem; line-height: 1.714285em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(218, 218, 218); position: relative !important; margin-bottom: 3em; margin-left: 2em; padding-left: 1ch; padding-right: 1ch; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">map(round, [1.2,3.5,1.7])
      3-element Array{Float64,1}:
      1.0
      4.0
      2.0

      map(x -> x^2 + 2x - 1, [1,3,-1])
      3-element Array{Int64,1}:
      2
      14
      -2</pre>

      <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="JULIA" cid="n105" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); --select-text-bg-color: #36284e; --select-text-font-color: #fff; font-size: 0.9rem; line-height: 1.714285em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(218, 218, 218); position: relative !important; margin-bottom: 3em; margin-left: 2em; padding-left: 1ch; padding-right: 1ch; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">julia> x[2]
      "hello"</pre>

      <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="julia" cid="n107" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); --select-text-bg-color: #36284e; --select-text-font-color: #fff; font-size: 0.9rem; line-height: 1.714285em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(218, 218, 218); position: relative !important; margin-bottom: 3em; margin-left: 2em; padding-left: 1ch; padding-right: 1ch; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">julia> x = (a=1, b=1+1)
      (a = 1, b = 2)

      julia> x.a
      1</pre>

      <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="julia" cid="n113" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); --select-text-bg-color: #36284e; --select-text-font-color: #fff; font-size: 0.9rem; line-height: 1.714285em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(218, 218, 218); position: relative !important; margin-bottom: 3em; margin-left: 2em; padding-left: 1ch; padding-right: 1ch; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">julia> z = begin
      x = 1
      y = 2
      x + y
      end
      3
      julia> z = (x = 1; y = 2; x + y)
      3</pre>

      <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="julia" cid="n142" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); --select-text-bg-color: #36284e; --select-text-font-color: #fff; font-size: 0.9rem; line-height: 1.714285em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(218, 218, 218); position: relative !important; margin-bottom: 3em; margin-left: 2em; padding-left: 1ch; padding-right: 1ch; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">julia> x = 0;

      julia> for i = 1:10
      local x # this is also the default
      x = i + 1
      end

      julia> x
      0
      println(————————————————————————————————————————————————————————————————————)
      julia> x=0
      0

      julia> for i = 1:10
      x = i + 1
      end

      julia> x
      11</pre>

      <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="julia" cid="n144" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); --select-text-bg-color: #36284e; --select-text-font-color: #fff; font-size: 0.9rem; line-height: 1.714285em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(218, 218, 218); position: relative !important; margin-bottom: 3em; margin-left: 2em; padding-left: 1ch; padding-right: 1ch; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">julia> for i = 1:10
      global z
      z = i
      end

      julia> z
      10</pre>

      <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="julia" cid="n159" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); --select-text-bg-color: #36284e; --select-text-font-color: #fff; font-size: 0.9rem; line-height: 1.714285em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(218, 218, 218); position: relative !important; margin-bottom: 3em; margin-left: 2em; padding-left: 1ch; padding-right: 1ch; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">julia> (1+2)::AbstractFloat
      ERROR: TypeError: in typeassert, expected AbstractFloat, got Int64

      julia> (1+2)::Int
      3

      julia> function foo()
      x::Int8 = 100
      x
      end
      foo (generic function with 1 method)

      julia> foo()
      100

      julia> typeof(ans)
      Int8</pre>

      <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="julia" cid="n162" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); --select-text-bg-color: #36284e; --select-text-font-color: #fff; font-size: 0.9rem; line-height: 1.714285em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(218, 218, 218); position: relative !important; margin-bottom: 3em; margin-left: 2em; padding-left: 1ch; padding-right: 1ch; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">julia> struct Foo
      bar
      baz::Int
      qux::Float64
      end
      julia> foo = Foo("Hello, world.", 23, 1.5) # 当像函数一样使用类型时,它被称为构造函数。有两个构造函数会被自动生成(这些构造函数称为默认构造函数)。
      Foo("Hello, world.", 23, 1.5)

      julia> typeof(foo)
      Foo

      julia> fieldnames(Foo)
      (:bar, :baz, :qux)

      julia> foo.bar
      "Hello, world."

      julia> foo.baz
      23

      julia> foo.qux
      1.5</pre>

      <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="julia" cid="n175" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); --select-text-bg-color: #36284e; --select-text-font-color: #fff; font-size: 0.9rem; line-height: 1.714285em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(218, 218, 218); position: relative !important; margin-bottom: 3em; margin-left: 2em; padding-left: 1ch; padding-right: 1ch; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">julia> foo.quz = 2
      ERROR: type Foo is immutable

      julia></pre>

      这一块太多了,先放下

      其他

      暂时用不到先不管

      抽象类型Abstract、原始类型、已声明的类型

      类型公用体是一种特殊的抽象类型。没用到,不看了。

      类型共用体

      这一块很重要,但是暂时没用到,用到回来看看。

      • 参数复合类型

      • 参数抽象类型元组类型

      • 变参元组类型

      • 具名元组类型

      • 单态类型

      • 参数原始类型

      参数类型julia语言的一个相当高级的特性。也是他的一个重要优点。包括:

      参数类型

      其他

      关于如何构造复合类型的实例还有很多要说的,但这种讨论依赖于参数类型方法,并且这是非常重要的,应该在专门的章节中讨论:构造函数

      <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="julia" cid="n178" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); --select-text-bg-color: #36284e; --select-text-font-color: #fff; font-size: 0.9rem; line-height: 1.714285em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(218, 218, 218); position: relative !important; margin-bottom: 3em; margin-left: 2em; padding-left: 1ch; padding-right: 1ch; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">[julia> mutable struct Bar
      baz
      qux::Float64
      end

      julia> bar = Bar("Hello", 1.5);

      julia> bar.qux = 2.0
      2.0

      julia> bar.baz = 1//2
      1//2](https://docs.juliacn.com/latest/base/base/#mutable%20struct) </pre>

      可变复合类型

      如果需要,可以使用关键字 mutable struct 声明可变复合对象:

      • 它可以更高效。某些 struct 可以被高效地打包到数组中,并且在某些情况下,编译器可以完全避免分配不可变对象。

      • 不可能违反由类型的构造函数提供的不变性。

      • 使用不可变对象的代码更容易推理。

      使用 struct 声明的对象都是不可变的,它们在构造后无法修改。一开始看来这很奇怪,但它有几个优点:

      符合类型常被称为structrecordobject。复合类型在许多语言中是唯一一种用户可定义的类型,也是 Julia 中最常用的用户定义类型。相当于C结构体和其他高级语言的类。

      复合类型

      ::用来给表达式和变量附加类型注释。如果类型断言非真,抛出一个异常,否则返回左侧的值。

      类型声明

      Julia 的类型系统设计得强大而富有表现力,却清晰、直观且不引人注目。许多 Julia 程序员可能从未感觉需要编写明确使用类型的代码。但是,某些场景的编程可通过声明类型变得更加清晰、简单、快速和健壮。

      所有具体类型都是最终的类型,并且只有抽象类型可以作为其超类型。这意味着不再有结构上的继承关系。(虽然起初看起来这可能过于严格,但它有许多有益的结果,但缺点却少得出奇。事实证明,能够继承行为比继承结构更重要,同时继承两者在传统的面向对象语言中导致了重大困难。)

      还有一个非常显著的特点是:

      Julia类型系统是动态的,但是通过允许指出某些变量具有特定类型,进而获得了静态类型系统的一些优点。

      julia结合了动态类型语言和静态类型语言的又是。

      类型

      变量只会被赋值一次,可以通过const关键词实现。一般情况下,一旦定义了一个常量,请不要想尽一切办法再去更改他。(虽然确实有不太正常的办法)

      常量

      目前没有用到,因此先不读。

      let 语句块

      大多数的关键字都会引入局部作用于,而beginif是例外。

      在局部作用域内,也可以通过global赋值一个全局变量。

      外部的变量进入局部作用域的时候,可以通过local x的方式来强制转变为局部变量。

      同python等。

      局部作用域

      通过using或者import,模块可以把其他模块的变量引入到他的作用域中。

      每一个模块都会引入一个去全局作用域

      全局作用域

      julia有函数、类型、方法、模块等等各种结构。

      变量作用域

      很像python,可以直接查

      异常处理

      同python

      重复、循环

      ? : 用法同python

      但是if代码块也会返回一个数值,同复合表达式一样,返回最后一个被执行的语句的返回值。

      同python

      条件表达式

      其他

      这一部分本来是很重要的,但是由于暂时用不到,就不细看了

      协程:Task

      • 在表达式 a && b 中,子表达式 b 仅当 atrue 的时候才会被执行。

      • 在表达式 a || b 中,子表达式 b 仅在 afalse 的时候才会被执行。

        为什么这么写呢?当atrue的时候,无论b的数值是多少,a||b一定是true。因此b就不用执行了(b可以是一个函数,这也说明了函数在julia中崇高的地位)

        a&&b a||b是同理的

      短路求值

      begin;都可以进行流程控制,这两个组间的值都是最后一个子表达式的数值。

      复合表达式

      流程控制

      像python一样可以多值返回

      其他

      元组可以具备名字(规则真的是越来越随意了…… = _ =!)

      一个元组是一个固定长度的容器,可以容纳任何值,但不可以被修改(是immutable的)。 元组通过圆括号和逗号来构造,其内容可以通过索引来访问:

      Tuples——元组

      匿名函数主要是把函数作为参数传入到另一个函数中,比如:

      julia中的函数可以做任何事情。

      匿名函数

    传递行为

    函数

    检查字符串中的某字符串是否可以找到:

    occursion:

    带上第三个参数就可以在指定位置处寻找字符L:

    findfirst:

    常见操作:

    级联:使用*号进行级联,但是不能使用+。也可以使用string(str1, str2,…, strn)的方式s

    字符串索引字符的时候,绝大部分情况下是通过1开始的。可以再[ ]中直接输入end 即可代表字符串长度。

    字符同其他高级语言。

    字符和字符串

    搜一下便可知

    舍入函数、除法函数、幂对数和平方根、三角、双曲等特殊常用函数

    加入链式比较

    符合运算符:+= -=等和其他高等语言都一样

    其他

    例如sin.(arr)

    julia提供初等函数供直接使用,同样使用.可以用于数组。

    初等函数

    但是给出了其他几个额外测试函数:

    逻辑大小比较和其他高等语言一样

    数值比较 |

    相关文章

      网友评论

          本文标题:基础Julia笔记

          本文链接:https://www.haomeiwen.com/subject/bokzlctx.html