API Reference
This page provides a complete list of all exported functions and types.
MultifractalTools.bin_data — Methodbin_data(data::AbstractMatrix, l::Integer) -> MatrixPerforms spatial coarse-graining (binning) of the data.
This function divides the input data matrix into non-overlapping square boxes of size l x l. It calculates the "measure" (probability) in each box by summing the abs2 of all elements within that box.
The abs2 (e.g., |ψ|^2) is used, as the multifractal measure μ is typically a probability distribution.
Arguments
data::AbstractMatrix: The input 2D data, assumed to be renormalized (though not strictly required).l::Integer: The side length of the square bins.
Returns
- A new
Matrix(namedBinnedData) of sizefloor.(size(data) ./ l), where each element is the sum ofabs2(data[...])within that bin.
Examples
```jldoctest julia> A = [1.0 1.0 2.0 2.0; 1.0 1.0 2.0 2.0; 3.0 3.0 4.0 4.0; 3.0 3.0 4.0 4.0] 4×4 Matrix{Float64}: 1.0 1.0 2.0 2.0 1.0 1.0 2.0 2.0 3.0 3.0 4.0 4.0 3.0 3.0 4.0 4.0
Bin with l=2
Box 1 (top-left): 1²+1²+1²+1² = 4
Box 2 (top-right): 2²+2²+2²+2² = 16
Box 3 (bottom-left): 3²+3²+3²+3² = 36
Box 4 (bottom-right): 4²+4²+4²+4² = 64
julia> MultifractalTools.bin_data(A, 2) 2×2 Matrix{Float64}: 4.0 16.0 36.0 64.0
MultifractalTools.compute_scaling_quantities — Methodcompute_scaling_quantities(data, qs; ls=Integer[], crop_to_best_fit=true, crop_ratio=0.1)Computes the core scaling quantities required for multifractal analysis.
This function calculates the partition function $Z(q, l)$, the entropy $S(q, l)$, and the average $\log(\mu)$ weighted by $\mu^q$ (which we call $Z'(q, l)$) for a given data matrix, across a range of q values and box sizes l.
Arguments
data::AbstractMatrix: The input 2D data (e.g., a wavefunction or probability distribution).qs::AbstractVector: A vector ofqvalues (the "moments") to analyze.
Keyword Arguments
ls::AbstractVector{<:Integer}: An optional vector of box sizeslto use. If left empty (default), the function will automatically determine box sizes based on thedatasize and other keyword arguments.crop_to_best_fit::Bool: Iftrue(default) andlsis empty, the function will find the largest "best" square size (with the most divisors) withincrop_ratioof the original data size. Iffalse, it uses the full data size.crop_ratio::Float64: The fraction of the data size to search for a "best" size (default:0.1). For a 1000x1000 matrix, it will search from 900-1000.
Returns
- A
NamedTuplecontaining:ls: The vector of box sizeslthat were used.Zqs: An(l, q)matrix of the partition functions, $Z(q, l) = \sum_i \mu_i^q$.Sqs: An(l, q)matrix of the information entropies, $S(q, l) = \sum_i p_i \log(p_i)$.ZPrimes: An(l, q)matrix of the weighted log averages, $Z'(q, l) = \sum_i p_i \log(\mu_i)$.
Examples
```jldoctest julia> data = rand(128, 128); julia> qs = obtainqs(-5, 5, 11); julia> scalingdata = computescalingquantities(data, qs; croptobestfit=false); julia> scalingdata.ls 8-element Vector{Int64}: 1 2 4 8 16 32 64 128
MultifractalTools.compute_spectrum — Methodcompute_spectrum(ScalingQuantities, qs, λ1, λ2) -> NamedTupleCalculates the multifractal spectrum exponents ($ au(q)$, $lpha(q)$, $f(lpha)$) by fitting the results from compute_scaling_quantities.
This function performs a linear regression in log-log space to find the scaling exponents. For example, the multifractal exponent $ au(q)$ is found by fitting the power law $Z(q, psilon) \sim psilon^{ au(q)}$, which becomes linear in log space: $\log(Z) = au(q) \log(psilon) + C$.
The function fits this linear relationship using the power_law_model for $ au(q)$, $lpha(q)$, and $f(lpha)$ simultaneously.
Arguments
ScalingQuantities::NamedTuple: TheNamedTupleoutput fromcompute_scaling_quantities.qs::AbstractVector: The vector ofqvalues. This must be the sameqsvector used to generate theScalingQuantities.λ1::Integer: The starting index (not value) fromScalingQuantities.lsto use for the linear fit.λ2::Integer: The ending index fromScalingQuantities.lsto use for the linear fit.
Returns
- A
NamedTuplecontaining the calculated spectra:qs: Theqvalues.τqs: The vector of multifractal exponents, $ au(q)$.αs: The vector of singularity spectrum $lpha(q)$.fs: The vector of singularity spectrum $f(lpha)$.
Examples
```jldoctest julia> data = rand(128, 128); julia> qs = MultifractalTools.obtainqs(-5, 5, 3); # -5.0, 0.0, 5.0 julia> scalingdata = MultifractalTools.computescalingquantities(data, qs); julia> scaling_data.ls 8-element Vector{Int64}: 1 2 4 8 16 32 64 128
We fit using the scaling range from index 2 (l=2) to index 6 (l=32)
julia> spectrum = computespectrum(scalingdata, qs, 2, 6); julia> spectrum.τqs 3-element Vector{Float64}: 1.9999999999999996 -0.0 -2.0
MultifractalTools.find_best_scaling_size — Methodfind_best_scaling_size(max_size::Integer, crop_ratio::Float64) -> NamedTupleFinds an optimal square size for box-counting analysis near max_size.
This function searches for an integer n in the range [floor(max_size * (1 - crop_ratio)), max_size] that has the maximum number of divisors. This is useful for maximizing the number of available scaling sizes (l values) for the analysis, which improves the quality of the linear fits.
Arguments
max_size::Integer: The largest possible size (e.g.,minimum(size(data))).crop_ratio::Float64: The fraction of themax_sizeto search within. For example,0.1searches in the top 10% of the range.
Returns
- A
NamedTuplewith two fields:size: The integernfound to have the most divisors.divisors: AVector{Int}of the divisors ofn.
Examples
```jldoctest
Search in the range [90, 100]
96 has 12 divisors: [1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 96]
100 has 9 divisors: [1, 2, 4, 5, 10, 20, 25, 50, 100]
julia> result = MultifractalTools.findbestscaling_size(100, 0.1);
julia> result.size 96
julia> result.divisors 12-element Vector{Int64}: 1 2 3 4 6 8 12 16 24 32 48 96
MultifractalTools.get_divisors — Methodget_divisors(n::Integer) -> Vector{Int}Finds all positive divisors of an integer n and returns them in a sorted vector.
The function computes divisors by iterating up to sqrt(n) for efficiency. It correctly handles n=0 (returning an empty list) and negative numbers (by taking the absolute value).
Arguments
n::Integer: The number to find the divisors of.
Returns
- A
Vector{Int}containing all divisors ofnin ascending order.
Examples
```jldoctest julia> MultifractalTools.get_divisors(100) 9-element Vector{Int64}: 1 2 4 5 10 20 25 50 100
julia> MultifractalTools.get_divisors(17) 2-element Vector{Int64}: 1 17
MultifractalTools.obtain_qs — Methodobtain_qs(qmin::Number, qmax::Number, num_q::Integer) -> Vector{Float64}Generates a vector of num_q q-values, linearly spaced between qmin and qmax.
Arguments
qmin: The minimum value for theqrange.qmax: The maximum value for theqrange.num_q: The number ofqvalues to generate.
Returns
- A
VectorofFloat64values.
Examples
```jldoctest julia> obtain_qs(-5, 5, 3) 3-element Vector{Float64}: -5.0 0.0 5.0
MultifractalTools.power_law_model — Methodpower_law_model(x, p)A simple linear model $f(x) = p_1 x + p_2$ used for fitting power-law data.
This function is intended to be passed to LsqFit.curve_fit to find scaling exponents from log-transformed data.
It represents a power law $y = C x^\alpha$ that has been transformed into log-log space: $\log(y) = \alpha \log(x) + \log(C)$.
In this model:
xcorresponds to the log-transformed independent variable (e.g., $\log(l)$).p[1]is the parameter for the slope (the scaling exponent $\alpha$).p[2]is the parameter for the y-intercept (the prefactor $\log(C)$).
Arguments
x: The independent variable (e.g.,log.(ls)).p: A 2-element vector of parameters[slope, intercept].
Returns
- The fitted value
p[1] .* x .+ p[2].
Examples
```jldoctest julia> using LsqFit
Create data for a line y = 2x + 1
julia> xdata = [1.0, 2.0, 3.0, 4.0]; julia> ydata = [3.0, 5.0, 7.0, 9.0]; julia> p0 = [1.0, 0.0]; # Initial guess for [slope, intercept]
julia> fit = curvefit(MultifractalTools.powerlawmodel, xdata, y_data, p0);
julia> fit.param 2-element Vector{Float64}: 2.0 1.0
MultifractalTools.renormalize_data — Methodrenormalize_data(data::AbstractMatrix)Normalizes the input data matrix according to its $L^2$-norm.
This ensures that the sum of the absolute squares of the elements is equal to 1. Mathematically, this computes: $D' = D / \sqrt{\sum |D_{ij}|^2}$
This is typically the first step in a multifractal analysis to treat the data as a probability distribution ($\|\psi\|^2 = 1$).
Arguments
data::AbstractMatrix: The input 2D data.
Returns
- A new
AbstractMatrixof the same size, with its elements scaled.
Examples
```jldoctest julia> A = [1.0 1.0; 1.0 1.0] 2×2 Matrix{Float64}: 1.0 1.0 1.0 1.0
julia> B = MultifractalTools.renormalize_data(A);
julia> sum(abs2.(B)) 1.0