Ignore:
Timestamp:
10/11/19 00:25:20 (5 years ago)
Author:
bdef
Message:

CHG: syntax cahnge to meet most of Pep8 requirement

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/classes/qmu/linear_equality_constraint.py

    r23716 r24213  
    33from MatlabArray import *
    44
     5
    56class linear_equality_constraint:
    6         '''
     7    '''
    78  constructor for the linear_equality_constraint class.
    89
    910  [lec] = linear_equality_constraint.linear_equality_constraint(args)
    10    lec  = linear_equality_constraint()
     11   lec = linear_equality_constraint()
    1112
    1213  where the required args are:
     
    2122  arguments constructs a new instance from the arguments.
    2223'''
    23         def __init__(self):
    24                 self.matrix     = np.array([[float('NaN')]])
    25                 self.target     = 0.
    26                 self.scale_type = 'none'
    27                 self.scale      = 1.
     24    def __init__(self):
     25        self.matrix = np.array([[float('NaN')]])
     26        self.target = 0.
     27        self.scale_type = 'none'
     28        self.scale = 1.
    2829
    29         @staticmethod
    30         def linear_equality_constraint(*args):
    31                 nargin = len(args)
     30    @staticmethod
     31    def linear_equality_constraint(*args):
     32        nargin = len(args)
    3233
    33                 #  create a default object
    34                 if nargin == 0:
    35                         return linear_equality_constraint()
     34        #  create a default object
     35        if nargin == 0:
     36            return linear_equality_constraint()
    3637
    37                 #  copy the object
    38                 elif nargin == 1:
    39                         if isinstance(args[0],linear_equality_constraint):
    40                                 lec = args[0]
    41                         else:
    42                                 raise RuntimeError('Object is a '+str(type(args[0]))+' class object, not "linear_equality_constraint"')
     38        #  copy the object
     39        elif nargin == 1:
     40            if isinstance(args[0], linear_equality_constraint):
     41                lec = args[0]
     42            else:
     43                raise RuntimeError('Object is a ' + str(type(args[0])) + ' class object, not "linear_equality_constraint"')
    4344
    44                 #  create the object from the input
    45                 else:
    46                         if (np.shape(args[0],1) == array_np.size(args[1:min(nargin,4)]) or np.shape(args[0],1) == 1):
    47                                 asizec = np.shape(args[1:min(nargin,4)])
    48                         elif (array_np.size(args[1:min(nargin,4)]) == 1):
    49                                 asizec = [np.shape(args[0],1),1]
    50                         else:
    51                                 raise RuntimeError('Matrix for object of class '+str(type(lec))+' has inconsistent number of rows.')
     45        #  create the object from the input
     46        else:
     47            if (np.shape(args[0], 1) == array_np.size(args[1:min(nargin, 4)]) or np.shape(args[0], 1) == 1):
     48                asizec = np.shape(args[1:min(nargin, 4)])
     49            elif (array_np.size(args[1:min(nargin, 4)]) == 1):
     50                asizec = [np.shape(args[0], 1), 1]
     51            else:
     52                raise RuntimeError('Matrix for object of class ' + str(type(lec)) + ' has inconsistent number of rows.')
    5253
    53                         lec = [linear_equality_constraint() for i in range(asizec[0]) for j in range(asizec[1])]
     54            lec = [linear_equality_constraint() for i in range(asizec[0]) for j in range(asizec[1])]
    5455
    55                         for i in range(np.size(lec)):
    56                                 if (np.shape(args[0])[0] > 1):
    57                                         lec[i].matrix             = args[0][i,:]
    58                                 else:
    59                                         lec[i].matrix            = args[0]
     56            for i in range(np.size(lec)):
     57                if (np.shape(args[0])[0] > 1):
     58                    lec[i].matrix = args[0][i, :]
     59                else:
     60                    lec[i].matrix = args[0]
    6061
    61                         if (nargin >= 2):
    62                                 for i in range(np.size(lec)):
    63                                         if (np.size(args[1]) > 1):
    64                                                 lec[i].target    = args[1][i]
    65                                         else:
    66                                                 lec[i].target    = args[1]
     62            if (nargin >= 2):
     63                for i in range(np.size(lec)):
     64                    if (np.size(args[1]) > 1):
     65                        lec[i].target = args[1][i]
     66                    else:
     67                        lec[i].target = args[1]
    6768
    68                         if (nargin >= 3):                                       
    69                                 for i in range(np.size(lec)):
    70                                         if (np.size(args[2]) > 1):
    71                                                 lec[i].scale_type = args[2][i]
    72                                         else:
    73                                                 lec[i].scale_type = str(args[2])
    74                                            
    75                         if (nargin >= 4):
    76                                 for i in range(np.size(lec)):
    77                                         if (np.size(args[3]) > 1):
    78                                                 lec[i].scale      = args[3][i]
    79                                         else:
    80                                                 lec[i].scale      = args[3]
     69            if (nargin >= 3):
     70                for i in range(np.size(lec)):
     71                    if (np.size(args[2]) > 1):
     72                        lec[i].scale_type = args[2][i]
     73                    else:
     74                        lec[i].scale_type = str(args[2])
    8175
    82                         if (nargin > 4):
    83                                 print('WARNING: linear_equality_constraint:extra_arg: Extra arguments for object of class '+str(type(lec))+'.')
     76            if (nargin >= 4):
     77                for i in range(np.size(lec)):
     78                    if (np.size(args[3]) > 1):
     79                        lec[i].scale = args[3][i]
     80                    else:
     81                        lec[i].scale = args[3]
    8482
    85                 return lec
    86                                            
     83            if (nargin > 4):
     84                print('WARNING: linear_equality_constraint:extra_arg: Extra arguments for object of class ' + str(type(lec)) + '.')
    8785
    88         def __repr__(self):
    89                 # display the object
    90                 string = '\n'
    91                 string += 'class "linear_equality_constraint" object = \n'
    92                 string += '        matrix: '  +str(self.matrix) + '\n'
    93                 string += '        target: '  +str(self.target) + '\n'
    94                 string += '    scale_type: '  +str(self.scale_type) + '\n'
    95                 string += '         scale: '  +str(self.scale) + '\n'
     86        return lec
    9687
    97                 return string
     88    def __repr__(self):
     89        # display the object
     90        string = '\n'
     91        string += 'class "linear_equality_constraint" object = \n'
     92        string += '        matrix: ' + str(self.matrix) + '\n'
     93        string += '        target: ' + str(self.target) + '\n'
     94        string += '    scale_type: ' + str(self.scale_type) + '\n'
     95        string += '         scale: ' + str(self.scale) + '\n'
    9896
    99         @staticmethod
    100         def prop_matrix(lec):
    101                 if type(lec) not in [list,np.ndarray]:
    102                         return lec.matrix
     97        return string
    10398
    104                 matrix = np.zeros(np.size(lec))
    105                 for i in range(np.size(lec)):
    106                         matrix[i,0:np.shape(lec[i].matrix)[1]] = lec[i].matrix[0,:]
     99    @staticmethod
     100    def prop_matrix(lec):
     101        if type(lec) not in [list, np.ndarray]:
     102            return lec.matrix
    107103
    108                 return matrix
     104        matrix = np.zeros(np.size(lec))
     105        for i in range(np.size(lec)):
     106            matrix[i, 0:np.shape(lec[i].matrix)[1]] = lec[i].matrix[0, :]
    109107
    110         @staticmethod
    111         def prop_lower(lec):
    112                 lower=[]
    113                 return lower
     108        return matrix
    114109
    115         @staticmethod
    116         def prop_upper(lec):
    117                 upper=[]
    118                 return upper
     110    @staticmethod
     111    def prop_lower(lec):
     112        lower = []
     113        return lower
    119114
    120         @staticmethod
    121         def prop_target(lec):
    122                 if type(lec) not in [list,np.ndarray]:
    123                         return lec.target
     115    @staticmethod
     116    def prop_upper(lec):
     117        upper = []
     118        return upper
    124119
    125                 target = np.zeros(np.shape(lec))
    126                 for i in range(np.size(lec)):
    127                         target[i] = lec[i].target
    128                
    129                 target = allequal(target,0.)
     120    @staticmethod
     121    def prop_target(lec):
     122        if type(lec) not in [list, np.ndarray]:
     123            return lec.target
    130124
    131                 return target
     125        target = np.zeros(np.shape(lec))
     126        for i in range(np.size(lec)):
     127            target[i] = lec[i].target
    132128
    133         @staticmethod
    134         def prop_stype(lec):
    135                 if type(lec) not in [list,np.ndarray]:
    136                         return lec.scale_type
     129        target = allequal(target, 0.)
    137130
    138                 stype = ['' for i in range(np.size(lec))]
    139                 for i in range(np.size(lec)):
    140                         stype[i] = str(lec[i].scale_type)
    141                
    142                 stype = allequal(stype,'none')
     131        return target
    143132
    144                 return stype
     133    @staticmethod
     134    def prop_stype(lec):
     135        if type(lec) not in [list, np.ndarray]:
     136            return lec.scale_type
    145137
    146         @staticmethod
    147         def prop_scale(lec):
    148                 if type(lec) not in [list,np.ndarray]:
    149                         return lec.scale
     138        stype = ['' for i in range(np.size(lec))]
     139        for i in range(np.size(lec)):
     140            stype[i] = str(lec[i].scale_type)
    150141
    151                 scale = np.zeros(np.shape(lec))
    152                 for i in range(np.size(lec)):
    153                         scale[i] = lec[i].scale
    154                
    155                 scale = allequal(scale,1.)
     142        stype = allequal(stype, 'none')
    156143
    157                 return scale
    158    
    159         @staticmethod
    160         def dakota_write(fidi,dvar):
    161                 # collect only the variables of the appropriate class
    162                 lec = [struc_type(i,'linear_equality_constraint','lec') for i in dvar]
     144        return stype
    163145
    164                 # write constraints
    165                 lclist_write(fidi,'linear_equality_constraints','linear_equality',lec)
     146    @staticmethod
     147    def prop_scale(lec):
     148        if type(lec) not in [list, np.ndarray]:
     149            return lec.scale
     150
     151        scale = np.zeros(np.shape(lec))
     152        for i in range(np.size(lec)):
     153            scale[i] = lec[i].scale
     154
     155        scale = allequal(scale, 1.)
     156
     157        return scale
     158
     159    @staticmethod
     160    def dakota_write(fidi, dvar):
     161        # collect only the variables of the appropriate class
     162        lec = [struc_type(i, 'linear_equality_constraint', 'lec') for i in dvar]
     163
     164        # write constraints
     165        lclist_write(fidi, 'linear_equality_constraints', 'linear_equality', lec)
Note: See TracChangeset for help on using the changeset viewer.