Resolved issue 6: mctp2rfc is now working for the example given

master
per.andreas.brodtkorb 14 years ago
parent afd3c87e22
commit 8ff8b2183e

@ -9,7 +9,7 @@ import numpy as np
from numpy import (abs, amax, any, logical_and, arange, linspace, atleast_1d, atleast_2d,
array, asarray, broadcast_arrays, ceil, floor, frexp, hypot,
sqrt, arctan2, sin, cos, exp, log, mod, diff, empty_like,
finfo, inf, pi, interp, isnan, isscalar, zeros, ones,
finfo, inf, pi, interp, isnan, isscalar, zeros, ones, linalg,
r_, sign, unique, hstack, vstack, nonzero, where, extract)
from scipy.special import gammaln
from scipy.integrate import trapz, simps
@ -648,64 +648,71 @@ def mctp2rfc(f_mM,f_Mm=None):
computes f_rfc = f_mM + F_mct(f_mM).
CALL: f_rfc = mctp2rfc(f_mM);
where
f_rfc = the rainflow matrix,
Parameters
----------
f_mM = the min2max Markov matrix,
f_Mm = the max2min Markov matrix,
Further optional input arguments;
Returns
-------
f_rfc = the rainflow matrix,
CALL: f_rfc = mctp2rfc(f_mM,f_Mm,paramm,paramM);
Example:
-------
>>> fmM = np.array([[ 0.0183, 0.0160, 0.0002, 0.0000, 0],
... [0.0178, 0.5405, 0.0952, 0, 0],
... [0.0002, 0.0813, 0, 0, 0],
... [0.0000, 0, 0, 0, 0],
... [ 0, 0, 0, 0, 0]])
>>> mctp2rfc(fmM)
array([[ 2.66998090e-02, 7.79970042e-03, 4.90607697e-07,
0.00000000e+00, 0.00000000e+00],
[ 9.59962873e-03, 5.48500862e-01, 9.53995094e-02,
0.00000000e+00, 0.00000000e+00],
[ 5.62297379e-07, 8.14994377e-02, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00],
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00],
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00]])
f_Mm = the max2min Markov matrix,
paramm = the parameter matrix defining discretization of minimas,
paramM = the parameter matrix defining discretization of maximas,
'''
# TODO: Check this: paramm and paramM are never used?????
if f_Mm is None:
f_Mm=f_mM
# if nargin<3
# paramm=[-1, 1 ,length(f_mM)];
# paramM=paramm;
# end
#
# if nargin<4
# paramM=paramm;
# end
f_mM = np.atleast_1d(f_mM)
f_Mm = f_mM.copy()
else:
f_mM, f_Mm = np.atleast_1d(f_mM, f_Mm)
N = max(f_mM.shape)
f_max = sum(f_mM,axis=1)
f_min = sum(f_mM, axis=0)
f_max = np.sum(f_mM, axis=1)
f_min = np.sum(f_mM, axis=0)
f_rfc = zeros((N, N))
f_rfc[N-1,0]=f_max[N-1]
f_rfc[1,N-1]=f_min[N-1]
f_rfc[N - 2, 0] = f_max[N - 2]
f_rfc[0, N - 2] = f_min[N - 2]
for k in range(2, N - 1):
for i in range(1,k-1):
AA = f_mM[N-k+1:N-k+i-1, k-i+1:k-1]
AA1 = f_Mm[N-k+1:N-k+i-1, k-i+1:k-1]
RAA = f_rfc[N-k+1:N-k+i-1, k-i+1:k-1]
nA = max(AA.shape);
MA = f_max[N-k+1:N-k+i-1]
mA = f_min[k-i+1:k-1]
for i in range(1, k):
AA = f_mM[N - 1 - k:N - 1 - k + i, k - i:k]
AA1 = f_Mm[N - 1 - k:N - 1 - k + i, k - i:k]
RAA = f_rfc[N - 1 - k:N - 1 - k + i, k - i:k]
nA = max(AA.shape)
MA = f_max[N - 1 - k:N - 1 - k + i]
mA = f_min[k - i:k]
SA = AA.sum()
SRA = RAA.sum()
DRFC = SA-SRA;
NT = min(mA[0]-sum(RAA[:,1]),MA[0]-sum(RAA[1,:])) # ?? check
DRFC = SA - SRA
NT = min(mA[0] - sum(RAA[:, 0]), MA[0] - sum(RAA[0, :])) # ?? check
NT = max(NT, 0) # ??check
if NT > 1e-6 * max(MA[0], mA[0]):
NN = MA-sum(AA,axis=1) # T
e = (mA-sum(AA, axis=0)) # T
NN = MA - np.sum(AA, axis=1) # T
e = (mA - np.sum(AA, axis=0)) # T
e = np.flipud(e)
PmM = np.rot90(AA)
PmM = np.rot90(AA.copy())
for j in range(nA):
norm=mA[nA-j+1]
norm = mA[nA - 1 - j]
if norm != 0:
PmM[j, :] = PmM[j, :] / norm
e[j] = e[j] / norm
@ -713,43 +720,43 @@ def mctp2rfc(f_mM,f_Mm=None):
#end
fx = 0.0;
if max(abs(e)) > 1e-6 and max(abs(NN)) > 1e-6 * max(MA[0], mA[0]):
PMm=AA1;
PMm = AA1.copy()
for j in range(nA):
norm=MA(j);
norm = MA[j]
if norm != 0:
PMm[j, :] = PMm[j, :] / norm;
#end
#end
PMm=fliplr(PMm)
PMm = np.fliplr(PMm)
A=PMm; B=PmM;
I=eye(A.shape)
A = PMm
B = PmM
if nA == 1:
fx = NN * (A / (1 - B * A) * e)
else:
fx=NN*(A*((I-B*A)\e)) #least squares
rh = np.eye(A.shape[0]) - np.dot(B, A)
fx = np.dot(NN, np.dot(A, linalg.solve(rh, e))) #least squares
#end
#end
f_rfc[N-k+1,k-i+1] = fx+DRFC
f_rfc[N - 1 - k, k - i] = fx + DRFC
# check2=[ DRFC fx]
# pause
else:
f_rfc[N-k+1,k-i+1]=0.;
f_rfc[N - 1 - k, k - i] = 0.0
#end
#end
m0 = max(0,f_min[0]-sum(f_rfc[N-k+2:N,1]));
M0 = max(0,Max(N-k+1)-sum(f_rfc[N-k+1,2:k]));
f_rfc[N-k+1,1] = min(m0,M0)
m0 = max(0, f_min[0] - np.sum(f_rfc[N - k + 1:N, 0]))
M0 = max(0, f_max[N - 1 - k] - np.sum(f_rfc[N - 1 - k, 1:k]))
f_rfc[N - 1 - k, 0] = min(m0, M0)
#% n_loops_left=N-k+1
#end
for k in range(1, N):
M0 = max(0,f_max[0]-sum(f_rfc[1,N-k+2:N]));
m0 = max(0,f_min[N-k+1]-sum(f_rfc[2:k,N-k+1]));
f_rfc[1,N-k+1] = min(m0,M0)
M0 = max(0, f_max[0] - np.sum(f_rfc[0, N - k:N]));
m0 = max(0, f_min[N - 1 - k] - np.sum(f_rfc[1:k+1, N - 1 - k]));
f_rfc[0, N - 1 - k] = min(m0, M0)
#end
# %clf
@ -767,14 +774,7 @@ def mctp2rfc(f_mM,f_Mm=None):
# %axis([paramm(1) paramm(2) paramM(1) paramM(2)])
# %axis('square')
return f_frfc
return f_rfc

Loading…
Cancel
Save