types chapter: small changes

This commit is contained in:
Meik Hellmund 2024-05-08 14:34:54 +02:00
parent 537498a293
commit 1d55f19ce2
4 changed files with 307 additions and 66 deletions

View File

@ -21,6 +21,11 @@ z2 = PComplex1{Float32}(12, 13)
@show z1 z2; @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: Julia stellt automatisch *default constructors* zur Verfügung:
- den Konstruktor `PComplex1`, bei dem der Typ `T` von den übergebenen Argumenten abgeleitet wird und - den Konstruktor `PComplex1`, bei dem der Typ `T` von den übergebenen Argumenten abgeleitet wird und
@ -62,7 +67,7 @@ end
#| output: false #| 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.)) zz = @which Base.show(stdout, PComplex{Float64}(2.,3.))
if zz.module != Base 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. 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<tab>` eingegeben werden. Das Winkel-Zeichen `∠` steht leider nicht als Operatorsymbol zur Verfügung. Wir weichen aus auf `⋖`. Das kann in Julia als als `\lessdot<tab>` eingegeben werden.

View File

@ -114,6 +114,7 @@ abstract type MySuperType end
supertype(MySuperType) supertype(MySuperType)
``` ```
oder von einem anderen abstrakten Typ: oder von einem anderen abstrakten Typ:
```{julia} ```{julia}
@ -122,6 +123,8 @@ abstract type MySpecialNumber <: Integer end
supertypes(MySpecialNumber) supertypes(MySpecialNumber)
``` ```
Mit der Definition werden die abstrakten Typen an einer Stelle des Typ-Baums "eingehängt".
## Die numerischen Typen `Bool` und `Irrational` ## Die numerischen Typen `Bool` und `Irrational`
Da sie im Baum der numerischen Typen zu sehen sind, seien sie kurz erklärt: Da sie im Baum der numerischen Typen zu sehen sind, seien sie kurz erklärt:

View File

@ -19,10 +19,19 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"id": "3ee9c4ae", "id": "3ee9c4ae",
"metadata": {}, "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": [ "source": [
"struct PComplex1{T <: AbstractFloat} <: Number\n", "struct PComplex1{T <: AbstractFloat} <: Number\n",
" r :: T\n", " r :: T\n",
@ -59,7 +68,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 2,
"id": "214480f6", "id": "214480f6",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -102,10 +111,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 3,
"id": "95c7386c", "id": "95c7386c",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"PComplex{Float64}(3.3, 1.0)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"z1 = PComplex{Float64}(-3.3, 7π+1)" "z1 = PComplex{Float64}(-3.3, 7π+1)"
] ]
@ -122,10 +142,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 4,
"id": "e87d3a42", "id": "e87d3a42",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"PComplex{Float64}(2.0, 0.3)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"PComplex(r::T, ϕ::T) where {T<:AbstractFloat} = PComplex{T}(r,ϕ)\n", "PComplex(r::T, ϕ::T) where {T<:AbstractFloat} = PComplex{T}(r,ϕ)\n",
"\n", "\n",
@ -164,10 +195,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 5,
"id": "81818d42", "id": "81818d42",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"PComplex{Float64}(2.0, 1.5707963267948966)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
" ⋖(r::Real, ϕ::Real) = PComplex(r, π*ϕ/180)\n", " ⋖(r::Real, ϕ::Real) = PComplex(r, π*ϕ/180)\n",
"\n", "\n",
@ -187,10 +229,18 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 6,
"id": "4cf73e28", "id": "4cf73e28",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"z3 = 2.0⋖90.0°\n"
]
}
],
"source": [ "source": [
"using Printf\n", "using Printf\n",
"\n", "\n",
@ -229,10 +279,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 7,
"id": "042418fd", "id": "042418fd",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"Main"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"f(x) = 3x^3\n", "f(x) = 3x^3\n",
"@which f" "@which f"
@ -240,10 +301,18 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 27,
"id": "c20d10e5", "id": "c20d10e5",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Modul für Addition: Base, Modul für sqrt: Base\n"
]
}
],
"source": [ "source": [
"wp = @which +\n", "wp = @which +\n",
"ws = @which(sqrt)\n", "ws = @which(sqrt)\n",
@ -260,17 +329,28 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 28,
"id": "a53f0e79", "id": "a53f0e79",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"qwurzel (generic function with 1 method)"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"qwurzel(z::PComplex) = PComplex(sqrt(z.r), z.ϕ / 2)" "qwurzel(z::PComplex) = PComplex(sqrt(z.r), z.ϕ / 2)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 29,
"id": "d6987594", "id": "d6987594",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -297,10 +377,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 30,
"id": "f93f63c6", "id": "f93f63c6",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"19"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"length(methods(sqrt))" "length(methods(sqrt))"
] ]
@ -315,10 +406,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 32,
"id": "b88cd74b", "id": "b88cd74b",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"20"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"Base.sqrt(z::PComplex) = qwurzel(z)\n", "Base.sqrt(z::PComplex) = qwurzel(z)\n",
"\n", "\n",
@ -327,10 +429,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 33,
"id": "e077b8bf", "id": "e077b8bf",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"1.4142135623730951⋖8.6°"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"sqrt(z2)" "sqrt(z2)"
] ]
@ -345,10 +458,18 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 13,
"id": "5537f7ec", "id": "5537f7ec",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"z1 * z2 = 6.6⋖74.5°\n"
]
}
],
"source": [ "source": [
"Base.:*(x::PComplex, y::PComplex) = PComplex(x.r * y.r, x.ϕ + y.ϕ)\n", "Base.:*(x::PComplex, y::PComplex) = PComplex(x.r * y.r, x.ϕ + y.ϕ)\n",
"\n", "\n",
@ -372,10 +493,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 14,
"id": "610a38fe", "id": "610a38fe",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"265.53333333333336"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"1//3 + 5 + 5.2 + 0xff" "1//3 + 5 + 5.2 + 0xff"
] ]
@ -407,20 +539,40 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 15,
"id": "48cae0ae", "id": "48cae0ae",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"(12.0, 34.555, 0.7777777777777778, 255.0)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"promote(12, 34.555, 77/99, 0xff)" "promote(12, 34.555, 77/99, 0xff)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 16,
"id": "768934ac", "id": "768934ac",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"z = (33, 27)\n",
"typeof(z) = Tuple{BigInt, BigInt}\n"
]
}
],
"source": [ "source": [
"z = promote(BigInt(33), 27)\n", "z = promote(BigInt(33), 27)\n",
"@show z typeof(z);" "@show z typeof(z);"
@ -444,10 +596,18 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 17,
"id": "70306adc", "id": "70306adc",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"promote_type(Rational{Int64}, ComplexF64, Float32) = ComplexF64\n"
]
}
],
"source": [ "source": [
"@show promote_type(Rational{Int64}, ComplexF64, Float32);" "@show promote_type(Rational{Int64}, ComplexF64, Float32);"
] ]
@ -465,30 +625,69 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 18,
"id": "b326a121", "id": "b326a121",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"3.0"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"z = convert(Float64, 3)" "z = convert(Float64, 3)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 19,
"id": "57606643", "id": "57606643",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"23"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"z = convert(Int64, 23.00)" "z = convert(Int64, 23.00)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 20,
"id": "a3b39504", "id": "a3b39504",
"metadata": {}, "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": [ "source": [
"z = convert(Int64, 2.3)" "z = convert(Int64, 2.3)"
] ]
@ -525,10 +724,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 21,
"id": "e258330e", "id": "e258330e",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"PComplex"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"## (a) r, ϕ beliebige Reals, z.B. Integers, Rationals\n", "## (a) r, ϕ beliebige Reals, z.B. Integers, Rationals\n",
"\n", "\n",
@ -566,10 +776,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 22,
"id": "ec747779", "id": "ec747779",
"metadata": {}, "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": [ "source": [
"3//5 ⋖ 45, PComplex(Complex(1,1)), PComplex(-13) " "3//5 ⋖ 45, PComplex(Complex(1,1)), PComplex(-13) "
] ]
@ -586,7 +807,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 24,
"id": "806fff6a", "id": "806fff6a",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -616,20 +837,42 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 25,
"id": "cc47939d", "id": "cc47939d",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"(2.0⋖90.0°, 6.0⋖90.0°)"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"z3, 3z3" "z3, 3z3"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 26,
"id": "63c20bc7", "id": "63c20bc7",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"(43.26661530556787⋖64.0°, 16.970562748477143⋖8.6°)"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"(3.0+2im) * (12⋖30.3), 12sqrt(z2) " "(3.0+2im) * (12⋖30.3), 12sqrt(z2) "
] ]
@ -733,6 +976,12 @@
"display_name": "Julia 1.10.2", "display_name": "Julia 1.10.2",
"language": "julia", "language": "julia",
"name": "julia-1.10" "name": "julia-1.10"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.10.2"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -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(