歡迎訪問 Forcal程序設計

復雜的多重循環計算

例子1 含復數的多重循環計算并繪圖:

    要計算一個關于K,L,kama,alpha從負無窮到正無窮的求和(可取-10到10)(公式見下圖),并且分別獨立變化,這四個同時都在貝塞爾函數中不能分離。

    說明:m'=0,n'=0,a=1,b=1,c=1,d=1,Z=1,μ=9,ν=9。取m=4,n=4。
    φlimm,n,m',n'的值對應的是圖中每個點的取值。例如φlim0,0,m',n'就是圖中m=0,n=0的那個點(圖中m為橫坐標,n為縱坐標)。

    Forcal代碼(代碼中a0取10):

//定義函數cxy,是一個復數函數
c: cxy(c,x,y)= c=i^x*y;
//定義函數main
main(:m1,n1,a0,a,b,c,d,Z,k,m,n,l,alpha,kama,miu,you,ii,x,y,s,static,free:Array,sizemax)=
{
    if[free,delete(Array),return(0)],
    ii=9,
    Array=new[rtoi(real_s),rtoi(ii),rtoi(ii),rtoi(3)],
    m=0,(m<ii).while{
        n=0,(n<ii).while{
            Array.XSLSF::setrai[m,n,0,0], Array.XSLSF::setrai[m,n,1,0],
//數組初始化
            n++
        },
        m++
    },
    m1=0,n1=0,a0=10,a=1,b=1,c=1,d=1,
    Z=1,miu=9,you=9,s=0,
    m=-4,(m<=4).while{
        n=-4,(n<=4).while{
            k=-10,(k<=10).while{
                l=-10,(l<=10).while{
                    alpha=-10,(alpha<=10).while{
                        kama=-10,(kama<=10).while{
                            s = a0*(-1)^(alpha*miu+kama*you+k)*XSLSF::bessel1(k,2*c*Z)*XSLSF::bessel1(l,2*c*Z)
                                *[(-1)^(-m1-k-l)*XSLSF::bessel1(m-m1-k-l+2*alpha*(miu+2),2*a*Z)+XSLSF::bessel1(m+m1+k+l+2+2*alpha*(miu+2),2*a*Z)]
                                *[(-1)^(-n1-k+l)*XSLSF::bessel1(n-n1-k+l+2*kama*(you+2),2*b*Z)+XSLSF::bessel1(n+n1+k-l+2+2*kama*(you+2),2*b*Z)],
                            cxy[&x,&y,m+n+m1+n1+k+l,0,s,0],
//調用復數函數cxy
                            x=x+Array.XSLSF::getrai[m+4,n+4,0], Array.XSLSF::setrai[m+4,n+4,0,x],
//保存到數組
                            y=y+Array.XSLSF::getrai[m+4,n+4,1], Array.XSLSF::setrai[m+4,n+4,1,y],
                            kama++
                        },
                        alpha++
                    },
                    l++
                },
                k++
            },
            n++
        },
        m++
    },
    sizemax=0,
    m=0,(m<ii).while{
        n=0,(n<ii).while{
            s=sqrt{Array.XSLSF::getrai[m,n,0]^2+Array.XSLSF::getrai[m,n,1]^2},
//計算絕對值
            if[s>sizemax, sizemax=s],  
//找到最大值
            Array.XSLSF::setrai[m,n,2,s],
            n++
        },
        m++
    }
};
gleDrawScene[HFor("Scene")],stop();    
//設置場景繪制函數后退出
initScene(::球)=                       
//定義初始化函數
{
    球=gluNewQuadric[],                
//創建二次曲面對象
    gluQuadricDrawStyle[球,GLU_FILL],  
//設置二次曲面繪制模式
    gluQuadricNormals[球,GLU_SMOOTH]   
//設置二次曲面法線信息
};
Scene(:m,n,s,ii:球,Array,sizemax)=
{
    if[gleIsInitScene(),initScene()],  
//需要初始化場景時,就執行初始化函數
    glClear[],                         
//清屏和清除深度緩沖區
    glLoadIdentity(),                  
//重置當前Modelview矩陣
    glTranslated(0,0,-15),             
//向屏幕里移動15個單元
    glRotated[gleAngle(1),1,0,0],      
//使圖象沿X軸旋轉
    glRotated[gleAngle(2),0,1,0],      
//使圖象沿Y軸旋轉
    glRotated[gleAngle(3),0,0,1],      
//使圖象沿Z軸旋轉
    ii=9,
    m=0,(m<ii).while{
        n=0,(n<ii).while{
            s=Array.XSLSF::getrai[m,n,2]/sizemax,
//獲取數值并歸一化
            glPushMatrix(),                      
//將當前矩陣壓入堆棧
                glTranslated(m-4, n-4, 2*s),     
//移動坐標 ,可將2*s改為0,則z軸保持不變
                glColor3d(1.0,1-s,s),            
//顏色
                gluSphere[球,s/2,10,10],         
//繪制球
            glPopMatrix(),                       
//將當前矩陣彈出堆棧
            n++
        },
        m++
    }
};

例子2 趣味填字:用1到18這18個數組成兩個方程組等式如下,每個數只能也必須用一次,如何求解?

        ( )/( )( )+( )/( )( )+( )/( )( )=1
        ( )/( )( )+( )/( )( )-( )/( )( )=0.5

    說明:2  8 12 10 14  5 13  1  4  7 15  6 18  3  9 11 17 16 是一組解,則:

        2/812+10/145+13/14=1
        7/156+18/39-11/1716=0.5

    Forcal代碼:

main(:A,i,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,i17,i18,s)=
{
    A="\[19]",
    i=1,(i<=18).while{setw(A+i,i++)},
//Unicode靜態字符數組初始化
    i=0,
    i1=1,(i1<=16).while{
        setw(A+i1,0),
//setw(A+i1,0)將地址A+i1的單元置0
        i2=1,(i2<=18).while{
            if[!getw(A+i2),i2++,continue()],
//getw(A+i2)得到地址A+i2的單元的值
            setw(A+i2,0),
            i3=1,(i3<=18).while{
                if[!getw(A+i3),i3++,continue()],
                if{i1/[which(i3<=9,i2*10,i2*100)+i3]>=1,i3++,continue()},
                setw(A+i3,0),
                i4=1,(i4<=18).while{
                    if[!getw(A+i4) | getw(A+i4)<i1,i4++,continue()],
//單元A+i4為0時不能進入循環;getw(A+i4)<i1時不進入循環,可去掉重復解
                    setw(A+i4,0),
                    i5=1,(i5<=18).while{
                        if[!getw(A+i5),i5++,continue()],
                        setw(A+i5,0),
                        i6=1,(i6<=18).while{
                            if[!getw(A+i6),i6++,continue()],
                            if{i1/[which(i3<=9,i2*10,i2*100)+i3]+i4/[which(i6<=9,i5*10,i5*100)+i6]>=1,i6++,continue()},
//which是一個選擇計算函數
                            setw(A+i6,0),
                            i7=1,(i7<=18).while{
                                if[!getw(A+i7) | getw(A+i7)<i4,i7++,continue()],
                                setw(A+i7,0),
                                i8=1,(i8<=18).while{
                                    if[!getw(A+i8),i8++,continue()],
                                    setw(A+i8,0),
                                    i9=1,(i9<=18).while{
                                        if[!getw(A+i9),i9++,continue()],
                                        if{abs{i1/[which(i3<=9,i2*10,i2*100)+i3]+i4/[which(i6<=9,i5*10,i5*100)+i6]+i7/[which(i9<=9,i8*10,i8*100)+i9]-1}>1e-12,i9++,continue()},
                                        setw(A+i9,0),
                                        i10=1,(i10<=18).while{
                                            if[!getw(A+i10),i10++,continue()],
                                            setw(A+i10,0),
                                            i11=1,(i11<=18).while{
                                                if[!getw(A+i11),i11++,continue()],
                                                setw(A+i11,0),
                                                i12=1,(i12<=18).while{
                                                    if[!getw(A+i12),i12++,continue()],
                                                    setw(A+i12,0),
                                                    i13=1,(i13<=18).while{
                                                        if[!getw(A+i13) | getw(A+i13)<i10,i13++,continue()],
                                                        setw(A+i13,0),
                                                        i14=1,(i14<=18).while{
                                                            if[!getw(A+i14),i14++,continue()],
                                                            setw(A+i14,0),
                                                            i15=1,(i15<=18).while{
                                                                if[!getw(A+i15),i15++,continue()],
                                                                s=i10/[which(i12<=9,i11*10,i11*100)+i12]+i13/[which(i15<=9,i14*10,i14*100)+i15],
                                                                if{s<=0.5,i15++,continue()},
                                                                setw(A+i15,0),
                                                               
//
                                                                i16=1,(i16<=18).while{if[getw(A+i16),break()],i16++},
                                                                i17=i16+1,(i17<=18).while{if[getw(A+i17),break()],i17++},
                                                                i18=i17+1,(i18<=18).while{if[getw(A+i18),break()],i18++},
                                                                if{ abs{s-i16/[which(i18<=9,i17*10,i17*100)+i18]-0.5}<1e-12, i++,
                                                                    printff{"{1,i,3}{2,i,3}{3,i,3}{4,i,3}{5,i,3}{6,i,3}{7,i,3}{8,i,3}{9,i,3}{10,i,3}{11,i,3}{12,i,3}{13,i,3}{14,i,3}{15,i,3}{16,i,3}{17,i,3}{18,i,3}\r\n",i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,i17,i18}
                                                                },
                                                                if{ abs{s-i16/[which(i17<=9,i18*10,i18*100)+i17]-0.5}<1e-12, i++,
                                                                    printff{"{1,i,3}{2,i,3}{3,i,3}{4,i,3}{5,i,3}{6,i,3}{7,i,3}{8,i,3}{9,i,3}{10,i,3}{11,i,3}{12,i,3}{13,i,3}{14,i,3}{15,i,3}{16,i,3}{17,i,3}{18,i,3}\r\n",i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,i18,i17}
                                                                },
                                                                if{ abs{s-i17/[which(i16<=9,i18*10,i18*100)+i16]-0.5}<1e-12, i++,
                                                                    printff{"{1,i,3}{2,i,3}{3,i,3}{4,i,3}{5,i,3}{6,i,3}{7,i,3}{8,i,3}{9,i,3}{10,i,3}{11,i,3}{12,i,3}{13,i,3}{14,i,3}{15,i,3}{16,i,3}{17,i,3}{18,i,3}\r\n",i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i17,i18,i16}
                                                                },
                                                                if{ abs{s-i17/[which(i18<=9,i16*10,i16*100)+i18]-0.5}<1e-12, i++,
                                                                    printff{"{1,i,3}{2,i,3}{3,i,3}{4,i,3}{5,i,3}{6,i,3}{7,i,3}{8,i,3}{9,i,3}{10,i,3}{11,i,3}{12,i,3}{13,i,3}{14,i,3}{15,i,3}{16,i,3}{17,i,3}{18,i,3}\r\n",i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i17,i16,i18}
                                                                },
                                                                if{ abs{s-i18/[which(i17<=9,i16*10,i16*100)+i17]-0.5}<1e-12, i++,
                                                                    printff{"{1,i,3}{2,i,3}{3,i,3}{4,i,3}{5,i,3}{6,i,3}{7,i,3}{8,i,3}{9,i,3}{10,i,3}{11,i,3}{12,i,3}{13,i,3}{14,i,3}{15,i,3}{16,i,3}{17,i,3}{18,i,3}\r\n",i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i18,i16,i17}
                                                                },
                                                                if{ abs{s-i18/[which(i16<=9,i17*10,i17*100)+i16]-0.5}<1e-12, i++,
                                                                    printff{"{1,i,3}{2,i,3}{3,i,3}{4,i,3}{5,i,3}{6,i,3}{7,i,3}{8,i,3}{9,i,3}{10,i,3}{11,i,3}{12,i,3}{13,i,3}{14,i,3}{15,i,3}{16,i,3}{17,i,3}{18,i,3}\r\n",i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i18,i17,i16}
                                                                },
                                                               
//
                                                                setw(A+i15,i15++)
                                                            },
                                                            setw(A+i14,i14++)
                                                        },
                                                        setw(A+i13,i13++)
                                                    },
                                                    setw(A+i12,i12++)
                                                },
                                                setw(A+i11,i11++)
                                            },
                                            setw(A+i10,i10++)
                                        },
                                        setw(A+i9,i9++)
                                    },
                                    setw(A+i8,i8++)
                                },
                                setw(A+i7,i7++)
                            },
                            setw(A+i6,i6++)
                        },
                        setw(A+i5,i5++)
                    },
                    setw(A+i4,i4++)
                },
                setw(A+i3,i3++)
            },
            setw(A+i2,i2++)
        },
        setw(A+i1,i1++)
//setw(A+i1,i1++)將地址A+i1的單元置i1,然后i1++
    },
    i
//解的個數
};


版權所有© Forcal程序設計 2002-2010,保留所有權利
E-mail: [email protected]
  QQ:630715621
最近更新: 2010年02月03日

欧冠杯