-
Notifications
You must be signed in to change notification settings - Fork 1
/
Crecurrence.py
49 lines (44 loc) · 1.94 KB
/
Crecurrence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 6 08:03:14 2024
@author: Ido Schaefer
"""
import numpy as np
def recurrence_coefs(Cinit, Norders, f_rec, order_init=0, data_type=None):# Recurrence coefficient computation
"""
The function computes a set of coefficients which are defined by a
recurrence relation, from a set of initial coefficients Cinit and a
function which defines the recurrence rule.
Input:
Cinit (ndarray, 1D or 2D): A matrix; contains a set of initial coefficients which are
involved in the iterative process. The row index represents different
orders of the recursive process, and the column index represents the
different coeffcients of the same order in the recursive process.
Note: In order to avoid repeated memory allocation, the number of columns
should be the same as the final output matrix C.
Norders (int): The number of orders in the recursive process to be computed
f_rec (function object): A function of the form f_rec(previousC, prev_order);
computes a new order of coefficients from the previous orders.
Input of f_rec:
previousC (ndarray, 1D or 2D) : A matrix of the previous coefficients of the form of Cinit.
prev_order (int): The order of previousC
Output of f_rec: a row vector of the new coefficients.
order_init (int): The order of the coefficients in Cinit.
data_type (type): The data type of the output ndarray C.
Output:
C (2D ndarray): The set of all coeffcients, including Cinit; the row and
column index represent the same as in Cinit.
"""
if data_type is None:
# The default is the type of the input coefficients:
data_type = Cinit.dtype.type
if Cinit.ndim == 1:
Ncoefs = Cinit.size
Nrec = 1
else:
Nrec, Ncoefs = np.shape(Cinit)
C = np.zeros((Norders + Nrec, Ncoefs), dtype=data_type)
C[0:Nrec, :] = Cinit
for Ci in range(Nrec, (Nrec + Norders)):
C[Ci, :] = f_rec(C[(Ci - Nrec):Ci, :].squeeze(), order_init + Ci)
return C