Ejemplo n.º 1
0
def release_complex_code(code_strs, code_place, xde_dict, ges_dict):

    cplx_expr = code_strs.replace('Cplx_Asgn: ', '')
    left_var, righ_expr = cplx_expr.split('=')[:2]
    left_var, righ_expr = left_var.strip(), righ_expr.strip().strip(';')

    # if complex expres is a tensor expres, make summation first
    if left_var.find('_') != -1 \
    or righ_expr.find('_') != -1 :

        expr_list = idx_summation(left_var, righ_expr, xde_dict)

        for expres in expr_list:

            cplx_list = expres.split('=')
            cplx_objt = cmplx_expr(cplx_list[1])

            for ri, cmplexpr in zip(['r', 'i'], cplx_objt.complex_list):
                ges_dict['code'][code_place] \
                    .append(f'$cc {cplx_list[0]}{ri}={cmplexpr};\n')

    else:
        cplx_objt = cmplx_expr(righ_expr)

        for ri, cmplexpr in zip(['r', 'i'], cplx_objt.complex_list):
            ges_dict['code'][code_place] \
                .append(f'$cc {left_var}{ri}={cmplexpr};\n')
Ejemplo n.º 2
0
def release_tensor_code(code_strs, code_place, xde_dict, ges_dict):

    vect_expr = code_strs.replace('Tnsr_Asgn: ', '')

    left_var, righ_expr = vect_expr.split('=')[:2]
    left_var, righ_expr = left_var.strip(), righ_expr.strip().strip(';')

    expr_list = idx_summation(left_var, righ_expr, xde_dict)

    for expres in expr_list:
        if  expres.find('{') != -1 \
        and expres.find('}') != -1:
            ges_dict['code'][code_place].append('$cv ' + expres + ';\n')
        else:
            ges_dict['code'][code_place].append('$cc ' + expres + ';\n')
Ejemplo n.º 3
0
def parse_load(xde_dict, ges_dict):

    ges_dict['load'] = []

    left_var = 'load'
    righ_expr = ''.join(xde_dict['load'])
    expr_list = idx_summation(left_var, righ_expr, xde_dict)
    expr_list = split_bracket_expr(expr_list[0])

    for strs in expr_list[1:]:

        if 'cmplx_tag' in xde_dict and xde_dict['cmplx_tag'] == 1:

            weak_item = re.search(r'\[\w+\]', strs, re.I)
            cplx_item = re.search(r'\(?\|[+-]?\w+\;[+-]?\w+\|\)?', strs, re.I)

            if weak_item != None:
                weak_item = weak_item.group()

            if cplx_item != None:
                cplx_item = cplx_item.group()

            weak_var = weak_item.lstrip('[').rstrip(']')
            cplx_real_var, cplx_imag_var = cplx_item.lstrip('(').rstrip(
                ')').strip('|').split(';')

            weak_list = [weak_var + 'r', weak_var + 'i']
            cplx_list = [cplx_real_var, cplx_imag_var]

            for i in range(2):

                if re.match(r'[+-]?0(?:\.0*)?|[+-]?\.0+', cplx_list[i] ,re.I) != None \
                and cplx_list[i][-1] == '0' :
                    continue

                ges_dict['load'].append(strs.replace(weak_item, f'[{weak_list[i]}]') \
                                            .replace(cplx_item, f'({cplx_list[i]})') )

        else:
            ges_dict['load'].append(strs)
Ejemplo n.º 4
0
def parse_weak(weak, xde_dict, ges_dict):

    if xde_dict[weak][0].lower() != 'null' \
    or weak in ges_dict['code']:
        ges_dict[weak] = []

    else:
        return

    if xde_dict[weak][0] == 'dist':

        left_var = xde_dict[weak][0]
        righ_expr = ''

        for weak_strs in xde_dict[weak][1:]:
            righ_expr += weak_strs

        expr_list = idx_summation(left_var, righ_expr, xde_dict)
        expr_list = split_bracket_expr(expr_list[0])
        ges_dict[weak].append(expr_list[0].replace('=', ''))

        for strs in expr_list[1:]:

            if 'cmplx_tag' in xde_dict and xde_dict['cmplx_tag'] == 1:

                weak_item = re.search(r'\[\w+\;\w+\]', strs, re.I)
                cplx_item = re.search(r'\(?\|[+-]?\w+\;[+-]?\w+\|\)?', strs,
                                      re.I)

                if weak_item != None:
                    weak_item = weak_item.group()
                if cplx_item != None:
                    cplx_item = cplx_item.group()

                weak_left_var, weak_righ_var = weak_item.lstrip('[').rstrip(
                    ']').split(';')
                cplx_real_var, cplx_imag_var = cplx_item.lstrip('(').rstrip(
                    ')').strip('|').split(';')

                weak_left_list = [
                    weak_left_var + 'r', weak_left_var + 'i',
                    weak_left_var + 'r', weak_left_var + 'i'
                ]
                weak_righ_list = [
                    weak_righ_var + 'r', weak_righ_var + 'i',
                    weak_righ_var + 'i', weak_righ_var + 'r'
                ]
                cplx_list = [
                    cplx_real_var, cplx_real_var, cplx_imag_var, cplx_imag_var
                ]

                if cplx_list[2][0] == '+':
                    cplx_list[2] = cplx_list[2].replace('+', '-')

                elif cplx_list[2][0] == '-':
                    cplx_list[2] = cplx_list[2].replace('-', '+')

                else:
                    cplx_list[2] = '-' + cplx_list[2]

                for i in range(4):
                    if re.match(r'[+-]?0(?:\.0*)?|[+-]?\.0+', cplx_list[i] ,re.I) != None \
                    and cplx_list[i][-1] == '0' :
                        continue

                    ges_dict[weak].append(strs.replace(weak_item, f'[{weak_left_list[i]};{weak_righ_list[i]}]') \
                                               .replace(cplx_item, f'({cplx_list[i]})') )

            else:
                ges_dict[weak].append(strs)

    elif xde_dict[weak][0] == 'lump':

        if len(xde_dict[weak]) == 1:
            xde_dict[weak].append('1.0')

        if len(xde_dict[weak]) == 2: pass

        ges_dict[weak].append('lump')
        for shaps in xde_dict['shap']:
            nodn = re.search(r'\d+', shaps, re.I).group()

            for var in xde_dict['shap'][shaps]:
                for ii in range(int(nodn)):
                    ges_dict[weak].append(f'+[{xde_dict[weak][1]}]{var}{ii+1}')
Ejemplo n.º 5
0
def release_funcasgn_code(code_strs, code_place, xde_dict, ges_dict):

    tnsr_expr = code_strs.replace('Func_Asgn: ', '')
    left_var, righ_expr = tnsr_expr.split('=')[:2]
    left_var, righ_expr = left_var.strip(), righ_expr.strip()

    left_var_type = left_var.count('_')

    # assign the temporary list type of 'fvect' or 'fmatr'
    # to the list type of 'vect' or 'matrix'
    if  left_var[0]  != '[' \
    and left_var[-1] != ']' :

        left_var_name = left_var.split('_')[0]

        # Func_Asgn: a = b[*,*] --------- @W a b * *
        if re.search(r'[a-z]+[0-9a-z]*\[([0-9],)*[0-9]?\]$', righ_expr,
                     re.I) != None:

            # read right variable index list
            righ_var = righ_expr.split('[')[0]

            if  righ_expr[-1] == ']' \
            and righ_expr[-2] == '[':

                if   'fvect' in xde_dict \
                and righ_var in xde_dict['fvect']:
                    righ_idxs = list(range(len(xde_dict['fvect'][righ_var])))

                elif 'fmatr' in xde_dict \
                and righ_var in xde_dict['fmatr']:
                    righ_idxs = list(range(xde_dict['fvect'][righ_var][0] \
                                          *xde_dict['fvect'][righ_var][1]))

            else:
                righ_idxs = list(map(lambda x: int(x)-1 , \
                                    righ_expr.split('[')[1].rstrip(']').split(',') ))

            if left_var_type == scalar:
                expres = left_var + '='

                if   'fvect' in xde_dict \
                and righ_var in xde_dict['fvect']:
                    for idx in righ_idxs:
                        expres += xde_dict['fvect'][righ_var][idx]

                elif 'fmatr' in xde_dict \
                and righ_var in xde_dict['fmatr']:

                    row, clm = xde_dict['fmatr'][righ_var][:2]
                    fmatr = xde_dict['fmatr'][righ_var][2:]

                    for idx in righ_idxs:
                        expres += fmatr[int(idx / clm)][idx % clm]

                ges_dict['code'][code_place].append(expres + '\n\n')

            elif left_var_type == vector:

                expr_list = []

                for keyword in ['vect', 'array_vect']:
                    if keyword in xde_dict and left_var_name in xde_dict[
                            keyword]:
                        temp_list = xde_dict[keyword][left_var_name]

                if len(temp_list) == len(righ_idxs):

                    if 'fvect' in xde_dict and righ_var in xde_dict['fvect']:
                        for var, idx in zip(temp_list, righ_idxs):
                            expr_list.append( var + '=' + \
                                xde_dict['fvect'][righ_var][idx] + '\n\n')

                    elif 'fmatr' in xde_dict and righ_var in xde_dict['fmatr']:

                        row, clm = xde_dict['fmatr'][righ_var][:2]
                        fmatr = xde_dict['fmatr'][righ_var][2:]

                        for var, idx in zip(temp_list, righ_idxs):
                            expr_list.append(var + '=' + \
                                fmatr[int(idx/clm)][idx%clm] + '\n\n')

                ges_dict['code'][code_place] += expr_list

            elif left_var_type == matrix:

                expr_list = []
                for keyword in ['matrix', 'array_matrix']:
                    if keyword in xde_dict and left_var_name in xde_dict[
                            keyword]:
                        matr_len = xde_dict[keyword][left_var_name][0] \
                                 * xde_dict[keyword][left_var_name][1]

                temp_list = xde_dict['matrix'][left_var_name][2:]

                if matr_len == len(righ_idxs):

                    if 'fvect' in xde_dict \
                        and righ_var in xde_dict['fvect']:

                        idx = 0
                        for lists in temp_list:
                            for var in lists:
                                idx += 1
                                expr_list.append( var + '=' + \
                                    xde_dict['fvect'][righ_var][idx] + '\n\n')

                elif 'fmatr' in xde_dict and righ_var in xde_dict['fmatr']:

                    row, clm = xde_dict['fmatr'][righ_var][:2]
                    fmatr = xde_dict['fmatr'][righ_var][2:]

                    i = 0
                    for matr_row in temp_list:
                        for matr_vara in matr_row:
                            idx = righ_idxs[i]
                            expr_list.append( matr_vara + '=' + \
                                fmatr[int(idx/clm)][idx%clm] + '\n\n')
                            i += 1

                ges_dict['code'][code_place] += expr_list

    # assign the temporary list type of 'fvect' or 'fmatr'
    # to the list type of 'fvect' or 'fmatr'
    elif left_var[0]  == '[' \
    and  left_var[-1] == ']' :

        left_var = left_var.lstrip('[').rstrip(']')
        left_var_name = left_var.split('_')[0]

        # Func_Asgn: [a] = b[*,*] --------- @S a b * *
        if re.search(r'[a-z]+[0-9a-z]*\[([0-9],)*[0-9]?\]$', righ_expr,
                     re.I) != None:

            # read right variable index list
            righ_var = righ_expr.split('[')[0]

            if  righ_expr[-1] == ']' \
            and righ_expr[-2] == '[':

                if 'fvect' in xde_dict \
                and righ_var in xde_dict['fvect']:
                    righ_idxs = list(range(len(xde_dict['fvect'][righ_var])))

                elif 'fmatr' in xde_dict \
                and righ_var in xde_dict['fmatr']:
                    righ_idxs = list(range(xde_dict['fvect'][righ_var][0] \
                                          *xde_dict['fvect'][righ_var][1]))

            else:
                righ_idxs = list(map(lambda x: int(x)-1 , \
                                    righ_expr.split('[')[1].rstrip(']').split(',') ))

            if left_var_type == scalar:
                expres = left_var_name + '='

                if   'fvect' in xde_dict \
                and righ_var in xde_dict['fvect']:
                    for idx in righ_idxs:
                        expres += xde_dict['fvect'][righ_var][idx]

                elif 'fmatr' in xde_dict \
                and righ_var in xde_dict['fmatr']:

                    row, clm = xde_dict['fmatr'][righ_var][:2]
                    fmatr = xde_dict['fmatr'][righ_var][2:]

                    for idx in righ_idxs:
                        expres += fmatr[int(idx / clm)][idx % clm]

                ges_dict['code'][code_place].append(expres + '\n\n')

            elif left_var_type == vector:

                vect_len = len(xde_dict['fvect'][left_var_name])

                if vect_len == len(righ_idxs):

                    if   'fvect' in xde_dict \
                    and righ_var in xde_dict['fvect']:

                        for idx, ii in zip(righ_idxs, range(vect_len)):
                            xde_dict['fvect'][left_var_name][ii] = \
                            xde_dict['fvect'][left_var_name][idx]

                    elif 'fmatr' in xde_dict \
                    and righ_var in xde_dict['fmatr']:

                        row, clm = xde_dict['fmatr'][righ_var][:2]
                        for idx, ii in zip(righ_idxs, range(vect_len)):
                            xde_dict['fvect'][left_var_name][ii] = \
                            xde_dict['fmatr'][righ_var][2:][int(idx/clm)][idx%clm]

            elif left_var_type == matrix:

                matr_len = xde_dict['fmatr'][left_var_name][0] \
                         * xde_dict['fmatr'][left_var_name][1]
                lclm = xde_dict['fmatr'][left_var_name][1]

                if matr_len == len(righ_idxs):
                    if   'fvect' in xde_dict \
                    and righ_var in xde_dict['fvect']:

                        for ii, idx in zip(range(matr_len), righ_idxs):
                            xde_dict['fmatr'][left_var_name][2:][int(idx/clm)][ii%lclm] = \
                                xde_dict['fvect'][righ_var][idx]

                    elif 'fmatr' in xde_dict \
                    and righ_var in xde_dict['fmatr']:

                        row, clm = xde_dict['fmatr'][righ_var][:2]
                        for ii, idx in zip(range(matr_len), righ_idxs):
                            xde_dict['fmatr'][left_var_name][2:][int(idx/clm)][ii%lclm-1] = \
                                xde_dict['fmatr'][righ_var][2:][int(idx/clm)][idx%clm]

        else:
            righ_expr = righ_expr.replace('[', '').replace(']', '')

            expr_list = idx_summation(left_var, righ_expr, xde_dict)

            left_var_name = left_var.split('_')[0]

            if left_var_type == vector:

                row = len(xde_dict['fvect'][left_var_name])

                for ii in range(row):
                    xde_dict['fvect'][left_var_name][ii] = \
                        expr_list[ii].split('=')[1].replace('++','+').replace('-+','-')

            elif left_var_type == matrix:

                row, clm = xde_dict['fmatr'][left_var_name][:2]

                for ii in range(row):
                    for jj in range(clm):
                        xde_dict['fmatr'][left_var_name][ii+2][jj] = \
                            expr_list[ii*row+jj].split('=')[1].replace('++','+').replace('-+','-')