Print a docstring of a program to stdout in Julia - julia

Julia accepts ?sin on the command line to display a help text. I believe such help texts are implemented as docstrings. I would like to print such docstrings from my Julia program at runtime. How to do that?

julia> #doc sin
sin(x)
Compute sine of x, where x is in radians.
sin(A::AbstractMatrix)
Compute the matrix sine of a square matrix A.
If A is symmetric or Hermitian, its eigendecomposition (eigen) is used to compute the
sine. Otherwise, the sine is determined by calling exp.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> sin(fill(1.0, (2,2)))
2×2 Matrix{Float64}:
0.454649 0.454649
0.454649 0.454649

Related

Polar Plots in Julia

I came across a Rose plot obtained with Plots.jl package in Julia:
https://goropikari.github.io/PlotsGallery.jl/src/rose.html
Two things are not clear to me. The first one is what is Julia doing on the line:
θ = 0:2pi/n:2pi
It seems that the output is (lower limit):(bin size):(upper limit) but I haven't seen this type of arithmetics previously where two ranges are divided. The second thing is that I would like to obtain a histogram polar plot as it was done with R (Making a polar histogram in ggplot2), but I haven't found the documentation for line styles or how to do it in Plots.jl. Thanks.
Note that start:step:end is a common syntax in creating ranges. Let's dissect the line:
# `pi` is a reserved variable name in Julia
julia> pi
π = 3.1415926535897...
# A simple division
julia> 2pi/1
6.283185307179586
# Simple multiplication
julia> 2pi
6.283185307179586
So the 0:2pi/n:2pi creates an object of type StepRange that starts from 0 up to 2pi with steps of size 2pi/n.
In the case of desired plot, you can use the PlotlyJS.jl package. As they provided an example here. (Scroll down until you see "Polar Bar Chart")
I tested the code myself, and it's reproducible expectedly. Unfortunately, I don't know anything about the R language.
julia> using RDatasets, DataFrames, PlotlyJS
julia> df = RDatasets.dataset("datasets", "iris");
julia> sepal = df.SepalWidth;
julia> plot(
barpolar(
r=sepal
)
)
Results in:

How to plot complex numbers in Julia?

I was trying to plot complex numbers in Julia but I haven't found any good way to do it yet.
using PyPlot
nums = ComplexF64.([1,2,4],[2,2,-1])
polar.(Base.vect.(0.0,angle.(nums)),Base.vect.(0.0,abs.(nums)),marker="o")
One way is to plot the real and imaginary part as x and y
julia> using Plots
julia> d = [0.0000000+0.0000000im, 0.1111111+0.0000000im,
0.1666667+0.0962250im, 0.2222222+0.0000000im,
0.3333333+0.0000000im, 0.3888889+0.0962250im,
0.3333333+0.1924501im, 0.4444444+0.1924501im,
0.5000000+0.2886751im, 0.5555556+0.1924501im,
0.6666667+0.1924501im, 0.6111111+0.0962250im,
0.6666667+0.0000000im, 0.7777778+0.0000000im,
0.8333333+0.0962250im, 0.8888889+0.0000000im,
1.0000000+0.0000000im]
julia> plot(real(d),imag(d))
# or directly with plot(d)

Printing help? information from a function in Julia

Title summarises it pretty well. Is there a simple way to remotely display the help information for a function?
DispHelp = function(query_string)
# Enter help mode as you would by pressing `?` and query the given string
print(help_text)
return help_text
end
Any thoughts?
Might be possible.
julia-1.2> for f in [sin, cos, tan]
println(Base.doc(f))
end
```
sin(x)
```
Compute sine of `x`, where `x` is in radians.
```
sin(A::AbstractMatrix)
```
Compute the matrix sine of a square matrix `A`.
If `A` is symmetric or Hermitian, its eigendecomposition ([`eigen`](#ref)) is used to compute the sine. Otherwise, the sine is determined by calling [`exp`](#ref).
# Examples
```jldoctest
julia> sin(fill(1.0, (2,2)))
2×2 Array{Float64,2}:
0.454649 0.454649
0.454649 0.454649
```
```
cos(x)
```
Compute cosine of `x`, where `x` is in radians.
```
cos(A::AbstractMatrix)
```
Compute the matrix cosine of a square matrix `A`.
If `A` is symmetric or Hermitian, its eigendecomposition ([`eigen`](#ref)) is used to compute the cosine. Otherwise, the cosine is determined by calling [`exp`](#ref).
# Examples
```jldoctest
julia> cos(fill(1.0, (2,2)))
2×2 Array{Float64,2}:
0.291927 -0.708073
-0.708073 0.291927
```
```
tan(x)
```
Compute tangent of `x`, where `x` is in radians.
```
tan(A::AbstractMatrix)
```
Compute the matrix tangent of a square matrix `A`.
If `A` is symmetric or Hermitian, its eigendecomposition ([`eigen`](#ref)) is used to compute the tangent. Otherwise, the tangent is determined by calling [`exp`](#ref).
# Examples
```jldoctest
julia> tan(fill(1.0, (2,2)))
2×2 Array{Float64,2}:
-1.09252 -1.09252
-1.09252 -1.09252
```
With Julia you can read the code for everything, so a few hours looking through the source should lead to enlightenment.
To complete the daycaster's answer on how to display the help for built-in function.
If you want to display help on your own function, you have to write a doc string as follow, on this example function:
#Here is my docstring between triple quote, just before function declaration
"""
square(x)
Return the square of x, if it's a number. If it's a vector, return element-wise square of the vector
"""
function square(x)
#And now the body of my function
out = x.*x
end

Logarithmic scale in a negative domain in Plots.jl

Consider this minimal example
julia> using Plots
julia> pyplot()
julia> x = -100:0.01:100
julia> y = x
julia> plot(x, y, xscale = :log10)
This raises the following error:
DomainError:
log10 will only return a complex result if called with a complex argument. Try log10(complex(x)).
Of course, this is because the logarithm of negative numbers is not well defined. However, in python you can overcome this problem using the command plt.xscale('symlog'). Is there an equivalent command for Plots.jl?

Julia how to eval the sym

Help! I got a result in the form of sym. But it seems eval doesn't work. How can I get a numerical answer? Thanks.
#show BBias
#show eval(BBias)
#show typeof(eval(BBias))
BBias = -213.53387843501*cos(6) + 73.4119295548356*sin(6) - 50*sin(6)*cos(6) + 316.255048160247
eval(BBias) = -213.53387843501*cos(6) + 73.4119295548356*sin(6) - 50*sin(6)*cos(6) + 316.255048160247
typeof(eval(BBias)) = SymPy.Sym
What you have here is a symbolic SymPy expression, not a Julia expression. eval in Julia will only evaluate a Julia expression. The N function from SymPy will evaluate and expression down to it's floating point value.
You can do a typeof(BBias) to see what kind of an object this is. Since I don't know how you have generated that object, I cannot replicate it fully. But here is some simple example showing how to using SymPy from Julia
julia> using SymPy
julia> x=Sym("pi")
pi
julia> typeof(x)
SymPy.Sym
julia> y=sin(x)
0
julia> typeof(y)
SymPy.Sym
julia> typeof(eval(y))
SymPy.Sym
julia> z=N(y)
0
julia> typeof(z)
Int64
A detailed tutorial on using SymPy from Julia is available here: http://nbviewer.jupyter.org/github/jverzani/SymPy.jl/blob/master/examples/tutorial.ipynb

Resources