Fortran学习笔记目录

书接上回:Fortran学习笔记:02 流控制语句

数组(Array)

定义数组

一维数组

program main
implicit none
integer :: i
integer :: a(10)
do i=1,10,1
a(i)=i*i;
end do
do i=1,10,1
write(*,*) "i=",i,"i*i=",a(i)
end do
end

数组申明格式:

Datatype name(size)

注意,size必须为常数,数组索引默认从1开始

多维数组

最多可以声明7维数组,声明格式

integer :: a(D1,D2,D3,...,Dn)	!n维数组

其他数组声明方式

没有特别赋值的情况下,数组的默认索引是从1开始的,例如

integer :: a(3) 		!包含a(1),a(2),a(3)

自定义范围声明

integer :: a(0:2) 		!一维数组包含a(0),a(1),a(2)
integer :: b(2:3,-1:1) !二维数组b(2~3,-1~1)都可以使用

赋初值

直接赋初值

integer :: a(row,col)=0
program main
implicit none
integer ,parameter:: row=10
integer ,parameter:: col=10
integer :: a(row,col)=0
integer :: i,j
do i=1,row,1
do j=1,col,1
write(*,*) a(i,j)
end do
end do
end

对数组的操作

Fortran 95拥有强大的数组操作方法,简化许多for循环操作

等号赋值

integer :: a(10),b(10)
!!数组赋值!!
a=10
!等效于
do i=1,10,1
a(i)=10
end do
!!数组复制!!
b=a
!等效于
do i=1,10,1
b(i)=a(i)
end do

矩阵基本运算

\(+,-,*,/\)基本四则运算与Matlab中点乘、点除方法相同,均为数组对应元素进行四则运算

数组操作 意义
\(a=b+c\) \(a(i,j)=b(i,j)+c(i,j)\)矩阵对应位置数值相加
\(a=b-c\) \(a(i,j)=b(i,j)-c(i,j)\)矩阵对应位置数值相减
\(a=b*c\) \(a(i,j)=b(i,j)*c(i,j)\)矩阵对应位置数值相乘
\(a=b/c\) \(a(i,j)=b(i,j)/c(i,j)\)矩阵对应位置数值相除以
program main
implicit none
integer ,parameter:: row=10
integer ,parameter:: col=10
integer :: a(row,col)
real :: b(row,col),c(row,col)
integer :: i,j
a=10
b=.5
c=a*b
do i=1,row,1
do j=1,col,1
write(*,*) c(i,j)
end do
end do
end

矩阵切片运算

可以通过对矩阵中部分数据进行赋值操作,Matlab中矩阵切片方式继承了Fortran语言这一形式。

a(3,5)=5			!a(3),a(4),a(5)数值设置为5
a(3:)=5 !a(3)之后的数值设置为5
a(3:5)=(/3,4,5/) !a(3),a(4),a(5)数值分别设为3,4,5
a(1:3)=b(4:6) !a,b赋值
a(1:5:2)=3 !a(1),a(3),a(5)赋值为3,1~5增量为2
a(1:10)=a(10:1:-1) !翻转a integer :: a(5),b(5,5)
a(:)=b(:,2) !b中第二列赋值给a integer :: a(5,5),b(5,5,5)
a(:,:)=b(:,:,1)

注意在进行数组操作时,需要保证两边数组数目一致

WHERE 语句

where是Fortran95添加的功能,可以通过逻辑判断,对数组部分元素进行操作。例如以下例子,可以将a中<3的数组元素赋值给b

program main
implicit none
integer :: i
integer :: a(5)=(/(i,i=1,5)/)
integer :: b(5)=0
where(a<3)
b=a
end where
write(*,*) "a=",a
write(*,*) "b=",b
stop
end
 !输出
a= 1 2 3 4 5
b= 1 2 0 0 0

where也可以作嵌套,elsewhere(逻辑判断)

program main
implicit none
integer :: i
integer :: a(5)=(/(i,i=1,5)/)
integer :: b(5)=0
where(a<3)
b=1
elsewhere(a>4)
b=2
elsewhere
b=3
end where
write(*,*) "a=",a
write(*,*) "b=",b
end
 !输出
a= 1 2 3 4 5
b= 1 1 3 3 2

FORALL语句

forall是Fortran95中添加的功能

program main
implicit none
integer :: i
integer :: a(5)=0 forall(i=1:5:2) !1~5,增量为2
a(i)=5
end forall
write(*,*) "a=",a forall(i=1:5)
a(i)=i
end forall
write(*,*) "a=",a
stop
end
 !输出
a= 5 0 5 0 5
a= 1 2 3 4 5

对于二维数组,有相同的操作

program main
implicit none
integer :: i,j
integer :: a(5,5)=0 forall(i=1:5:1,j=1:5:1)
a(i,j)=i+j
end forall do i=1,5,1
write(*,*) a(:,i)
end do
stop
end
!输出
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10

还可以在forall中添加条件判断,语法为

forall(索引1,索引2,逻辑判断)
program main
implicit none
integer :: i,j
integer,parameter :: N = 5
integer :: a(N,N)=0 forall(i=1:N,j=1:N,i>j) a(i,j)=1 !上三角
forall(i=1:N,j=1:N,i==j) a(i,j)=2 !对角线
forall(i=1:N,j=1:N,i<j) a(i,j)=3 !下三角 write(*,"(5(5I5,/))") a
stop
end
!输出
2 1 1 1 1
3 2 1 1 1
3 3 2 1 1
3 3 3 2 1
3 3 3 3 2

forall可以嵌套多层,forall中可以使用where,但是where中不能使用forall,例如

program main
implicit none
integer :: i,j
integer,parameter :: N = 5
integer :: a(N,N)=0 forall(i=1:N)
forall(j=1:N)
a(i,j)=i+j
end forall
end forall
write(*,"(5(5I5,/))") a forall(i=1:N)
where(a(i,:)/=6)
a(i,:)=0
end where
end forall
write(*,"(5(5I5,/))") a
stop
end
!输出
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10 0 0 0 0 6
0 0 0 6 0
0 0 6 0 0
0 6 0 0 0
6 0 0 0 0

数组的存储规则

可变大小的数组

利用allocate与deallocate分配与释放内存

program main
implicit none
integer :: students
integer,allocatable :: a(:)
integer :: i write(*,*) "How many students:"
read(*,*) students
allocate(a(students)) !分配内存 write(*,*) "size of array = ",size(a)
deallocate(a) !释放内存
stop
end

测试程序(测试当前计算机所能分配的最大内存)

program main
implicit none
integer :: size_N=0,error=0
integer,parameter :: one_mb=1024*1024
character,allocatable :: a(:) do while(.true.)
size_N=size_N+one_mb
allocate(a(size_N),stat=error) !分配内存
if(error/=0) then
exit
end if
write(*,"('Allocate ',I10,'bytes')") size_N
write(*,"(F10.2,'MB used')") real(size_N)/one_mb
deallocate(a) !释放内存
end do stop
end

数组应用实例

冒泡排序

program main
implicit none
integer :: a(5)=(/10,2,5,4,6/)
integer :: i,j,temp
write(*,"(5(I5))") a
do i=1,5
do j=i+1,5
if(a(i)>a(j)) then
temp=a(i)
a(i)=a(j)
a(j)=temp
end if
end do
end do
write(*,"(5(I5))") a
stop
end program
!输出
10 2 5 4 6
2 4 5 6 10

Fortran学习笔记:03 数组(Array)的更多相关文章

  1. [学习笔记]JS 数组Array push相关问题

    前言: 今天用写了一个二维数组,都赋值为零,然后更新其中一个值,结果和预期是不一样,会整列的相同位置都是同一个值. 1.用Chrome的控制台样例如下: arrs[2][2] =1的赋值,竟然是三个数 ...

  2. Java学习笔记 03 数组

    一.数组的创建和使用 数组的创建和使用 >>创建方法1:先声明,再用new关键字分配内存(使用new关键字分配内存,整形数组中各个元素的初始值都为0) String str[]; str= ...

  3. PHP学习笔记之数组篇

    摘要:其实PHP中的数组和JavaScript中的数组很相似,就是一系列键值对的集合.... 转载请注明来源:PHP学习笔记之数组篇   一.如何定义数组:在PHP中创建数组主要有两种方式,下面就让我 ...

  4. (目录)Fortran学习笔记:开坑!!!

    前言:因为某些原因,需要使用Fortran编写程序,记录下Fortran语法学习过程中的部分笔记.在此开坑记录,立下Flag,"希望年末能够更新完" Fortran 学习笔记 陈橙 ...

  5. JavaScript学习笔记之数组(二)

    JavaScript学习笔记之数组(二) 1.['1','2','3'].map(parseInt) 输出什么,为什么? ['1','2','3'].map(parseInt)//[1,NaN,NaN ...

  6. Fortran学习笔记4(循环语句)

    Fortran学习笔记4 Fortran学习笔记4 逻辑运算 循环 Do语句 Do-While循环 循环控制 循环应用实例 逻辑运算 if命令需要和逻辑运算表达式搭配才能起到很好的效果.下面分别列出F ...

  7. SaToken学习笔记-03

    SaToken学习笔记-03 如果排版有问题,请点击:传送门 核心思想 所谓权限验证,验证的核心就是一个账号是否拥有一个权限码 有,就让你通过.没有?那么禁止访问! 再往底了说,就是每个账号都会拥有一 ...

  8. Fortran学习笔记:01 基本格式与变量声明

    Fortran学习笔记目录 01 基本格式与变量声明 格式 固定格式(Fixed Format):Fortran77 程序需要满足一种特定的格式要求,具体形式参考教材 自由格式(Free Format ...

  9. 机器学习实战(Machine Learning in Action)学习笔记————03.决策树原理、源码解析及测试

    机器学习实战(Machine Learning in Action)学习笔记————03.决策树原理.源码解析及测试 关键字:决策树.python.源码解析.测试作者:米仓山下时间:2018-10-2 ...

随机推荐

  1. 图解Java 垃圾回收机制

    摘要: Java技术体系中所提倡的 自动内存管理 最终可以归结为自动化地解决了两个问题:给对象分配内存 以及 回收分配给对象的内存,而且这两个问题针对的内存区域就是Java内存模型中的 堆区.关于对象 ...

  2. TCP模拟QQ聊天功能

    需求: 模拟qq聊天功能:实现客户端与服务器(一对一)的聊天功能,客户端首先发起聊天,输入的内容在服务器端和客户端显示,然后服务器端也可以输入信息,同样信息在客户端和服务端显示. 提示: 客户端 1) ...

  3. 三大操作系统对比使用之·Windows10

    时间:2018-10-29 记录:byzqy 本篇是一篇个人对Windows系统使用习惯.技巧和应用推荐的文档,在此记录.分享和后续查询备忘. 打开终端: Win+R,调出"运行" ...

  4. Nginx的高级使用

    1.概述 之前介绍过Nginx的简单使用,今天来聊聊Nginx的一些高级使用. 2.使用Nginx解决跨域问题 当公司存在多个域名时,两个不同的域名相互访问就会存在跨域问题. 或者在进行前端开发时,通 ...

  5. MyBatis学习总结(六)——Mybatis3.x与Spring4.x整合

    一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype:create -DgroupId=me.gacl -DartifactId=spring4-myba ...

  6. IT人计算机网络浅析

    LAN: 局域网 local Area Network WAN: 广域网 Wide Area Network WAN = LAN +LAN+....+LAN 多个LAN 组成 WAN OSI 七层协议 ...

  7. K8S 简介

    K8S架构与组件 kubectl: 是一个客户端管理工具,直接管理API server,提供请求给API server,中间有auth认证.用户使用kubectl命令来请求API Server接口完成 ...

  8. AOP快速入门

    一.概念 AOP面向切面编程,是函数式编程的延申,是对OOP的补充: 代理模式:拦截增强作用,增强功能: 1.java继承,纵向共性抽取, 2.横向切面AOP织入增强代码方式 二.原理是通过代理机制, ...

  9. Java匿名内部类只可使用一次的理解

    匿名内部类只能使用一次,就不能在使用. 为了验证"只能使用一次"的实际含义,首先在程序中实例化了两个对象.并且在相应的类中加入了实现接口的匿名内部类: package com.An ...

  10. Spring Cloud Gateway 学习+实践

    官网上给出的Spring Cloud Gateway特性如下图所示: 翻译过来就是: 基于 Spring Framework 5 ,Project Reactor 以及 Spring Boot 2.0 ...