From 1d55f19ce24cb4c187581eaafc009820539ace13 Mon Sep 17 00:00:00 2001 From: Meik Hellmund Date: Wed, 8 May 2024 14:34:54 +0200 Subject: [PATCH] types chapter: small changes --- chapters/pcomplex.qmd | 9 +- chapters/types.qmd | 3 + nb/pcomplex.ipynb | 347 ++++++++++++++++++++++++++++++++++------ patch-quarto-stacktrace | 14 -- 4 files changed, 307 insertions(+), 66 deletions(-) delete mode 100644 patch-quarto-stacktrace diff --git a/chapters/pcomplex.qmd b/chapters/pcomplex.qmd index 95e2be2..60be000 100644 --- a/chapters/pcomplex.qmd +++ b/chapters/pcomplex.qmd @@ -21,6 +21,11 @@ z2 = PComplex1{Float32}(12, 13) @show z1 z2; ``` +:::{.callout-warning collapse="true" .titlenormal} +## +Es ist nicht möglich, in einer Julia-Session eine einmal definierte `struct` später umzudefinieren. Daher verwende ich verschiedene Namen. Eine andere Möglichkeit ist z.B. die Verwendung von [`ProtoStructs.jl`](https://juliahub.com/ui/Packages/General/ProtoStructs). +::: + Julia stellt automatisch *default constructors* zur Verfügung: - den Konstruktor `PComplex1`, bei dem der Typ `T` von den übergebenen Argumenten abgeleitet wird und @@ -62,7 +67,7 @@ end #| output: false #= -in den ganzen quarto-runs wollen bir hier noch das default-show benutzen +in den ganzen quarto-runs wollen wir hier noch das default-show benutzen =# zz = @which Base.show(stdout, PComplex{Float64}(2.,3.)) if zz.module != Base @@ -101,8 +106,6 @@ In Julia ist eine große Anzahl von Unicode-Zeichen reserviert für die Verwendu Auf Details werden wir in einem späteren Kapitel noch eingehen. -Und ja, der Julia-Parser ist in einem Lisp(genauer: Scheme)-Dialekt geschrieben. In Julia ist ein kleiner Scheme-Interpreter namens [femtolisp](https://github.com/JeffBezanson/femtolisp) integriert. Geschrieben hat ihn einer der "Väter" von Julia bevor er mit der Arbeit an Julia begann. - ::: Das Winkel-Zeichen `∠` steht leider nicht als Operatorsymbol zur Verfügung. Wir weichen aus auf `⋖`. Das kann in Julia als als `\lessdot` eingegeben werden. diff --git a/chapters/types.qmd b/chapters/types.qmd index ff7abe4..197a5b6 100644 --- a/chapters/types.qmd +++ b/chapters/types.qmd @@ -114,6 +114,7 @@ abstract type MySuperType end supertype(MySuperType) ``` + oder von einem anderen abstrakten Typ: ```{julia} @@ -122,6 +123,8 @@ abstract type MySpecialNumber <: Integer end supertypes(MySpecialNumber) ``` +Mit der Definition werden die abstrakten Typen an einer Stelle des Typ-Baums "eingehängt". + ## Die numerischen Typen `Bool` und `Irrational` Da sie im Baum der numerischen Typen zu sehen sind, seien sie kurz erklärt: diff --git a/nb/pcomplex.ipynb b/nb/pcomplex.ipynb index edc33ee..e96abd8 100644 --- a/nb/pcomplex.ipynb +++ b/nb/pcomplex.ipynb @@ -19,10 +19,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "3ee9c4ae", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "z1 = PComplex1{Float64}(-32.0, 33.0)\n", + "z2 = PComplex1{Float32}(12.0f0, 13.0f0)\n" + ] + } + ], "source": [ "struct PComplex1{T <: AbstractFloat} <: Number\n", " r :: T\n", @@ -59,7 +68,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "214480f6", "metadata": {}, "outputs": [], @@ -102,10 +111,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "95c7386c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "PComplex{Float64}(3.3, 1.0)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "z1 = PComplex{Float64}(-3.3, 7π+1)" ] @@ -122,10 +142,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "e87d3a42", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "PComplex{Float64}(2.0, 0.3)" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "PComplex(r::T, ϕ::T) where {T<:AbstractFloat} = PComplex{T}(r,ϕ)\n", "\n", @@ -164,10 +195,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "81818d42", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "PComplex{Float64}(2.0, 1.5707963267948966)" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ " ⋖(r::Real, ϕ::Real) = PComplex(r, π*ϕ/180)\n", "\n", @@ -187,10 +229,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "4cf73e28", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "z3 = 2.0⋖90.0°\n" + ] + } + ], "source": [ "using Printf\n", "\n", @@ -229,10 +279,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "042418fd", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "Main" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "f(x) = 3x^3\n", "@which f" @@ -240,10 +301,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "id": "c20d10e5", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Modul für Addition: Base, Modul für sqrt: Base\n" + ] + } + ], "source": [ "wp = @which +\n", "ws = @which(sqrt)\n", @@ -260,17 +329,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "id": "a53f0e79", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "qwurzel (generic function with 1 method)" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "qwurzel(z::PComplex) = PComplex(sqrt(z.r), z.ϕ / 2)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "id": "d6987594", "metadata": {}, "outputs": [], @@ -297,10 +377,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "id": "f93f63c6", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "19" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "length(methods(sqrt))" ] @@ -315,10 +406,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "id": "b88cd74b", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "Base.sqrt(z::PComplex) = qwurzel(z)\n", "\n", @@ -327,10 +429,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "id": "e077b8bf", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1.4142135623730951⋖8.6°" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "sqrt(z2)" ] @@ -345,10 +458,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "id": "5537f7ec", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "z1 * z2 = 6.6⋖74.5°\n" + ] + } + ], "source": [ "Base.:*(x::PComplex, y::PComplex) = PComplex(x.r * y.r, x.ϕ + y.ϕ)\n", "\n", @@ -372,10 +493,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "id": "610a38fe", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "265.53333333333336" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "1//3 + 5 + 5.2 + 0xff" ] @@ -407,20 +539,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "id": "48cae0ae", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(12.0, 34.555, 0.7777777777777778, 255.0)" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "promote(12, 34.555, 77/99, 0xff)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "id": "768934ac", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "z = (33, 27)\n", + "typeof(z) = Tuple{BigInt, BigInt}\n" + ] + } + ], "source": [ "z = promote(BigInt(33), 27)\n", "@show z typeof(z);" @@ -444,10 +596,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "id": "70306adc", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "promote_type(Rational{Int64}, ComplexF64, Float32) = ComplexF64\n" + ] + } + ], "source": [ "@show promote_type(Rational{Int64}, ComplexF64, Float32);" ] @@ -465,30 +625,69 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "id": "b326a121", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "3.0" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "z = convert(Float64, 3)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "id": "57606643", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "23" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "z = convert(Int64, 23.00)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "id": "a3b39504", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "LoadError", + "evalue": "InexactError: Int64(2.3)", + "output_type": "error", + "traceback": [ + "InexactError: Int64(2.3)", + "", + "Stacktrace:", + " [1] Int64", + " @ ./float.jl:912 [inlined]", + " [2] convert(::Type{Int64}, x::Float64)", + " @ Base ./number.jl:7", + " [3] top-level scope", + " @ In[20]:1" + ] + } + ], "source": [ "z = convert(Int64, 2.3)" ] @@ -525,10 +724,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "id": "e258330e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "PComplex" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "## (a) r, ϕ beliebige Reals, z.B. Integers, Rationals\n", "\n", @@ -566,10 +776,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "id": "ec747779", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(0.6⋖45.0°, 1.4142135623730951⋖45.0°, 13.0⋖180.0°)" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "3//5 ⋖ 45, PComplex(Complex(1,1)), PComplex(-13) " ] @@ -586,7 +807,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "id": "806fff6a", "metadata": {}, "outputs": [], @@ -616,20 +837,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "id": "cc47939d", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(2.0⋖90.0°, 6.0⋖90.0°)" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "z3, 3z3" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "id": "63c20bc7", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "(43.26661530556787⋖64.0°, 16.970562748477143⋖8.6°)" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "(3.0+2im) * (12⋖30.3), 12sqrt(z2) " ] @@ -733,6 +976,12 @@ "display_name": "Julia 1.10.2", "language": "julia", "name": "julia-1.10" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "1.10.2" } }, "nbformat": 4, diff --git a/patch-quarto-stacktrace b/patch-quarto-stacktrace deleted file mode 100644 index 5c62a0c..0000000 --- a/patch-quarto-stacktrace +++ /dev/null @@ -1,14 +0,0 @@ -diff --git a/src/core/jupyter/jupyter.ts b/src/core/jupyter/jupyter.ts -index 4a1d04963..2fbda1d12 100644 ---- a/src/core/jupyter/jupyter.ts -+++ b/src/core/jupyter/jupyter.ts -@@ -1575,7 +1575,7 @@ function mdOutputStream(output: JupyterOutputStream) { - } - - function mdOutputError(output: JupyterOutputError) { -- return mdCodeOutput([output.ename + ": " + output.evalue]); -+ return mdCodeOutput([output.ename + ": " + output.traceback.join('\n')]); - } - - async function mdOutputDisplayData( -