fortran中的一些函数


函数:SIGN(A,B)

Description:

SIGN(A,B) returns the value of  A with the sign of  B
Standard:
Fortran 77 and later 
Class:
Elemental function 
Syntax:
RESULT = SIGN(A, B) 
Arguments:

AShall be of type INTEGER or REAL 
BShall be of the same type and kind as A 
SIGN(A,B)以B的符号返回A的值。返回值的符号位B的符号,返回值的绝对值为A的绝对值


print *,sign(1,-8)
print *,sign(9,-5)
print *,sign(-3,2)

输出分别为-1,-9,3




MAXLOC — Location of the maximum value within an array

MAXLOC函数返回最大值在数组中的位置
当没有指定位维数参数的时候,返回值的维数等于数组的rank

Description:

Determines the location of the element in the array with the maximum value, or, if the  DIM argument is supplied, determines the locations of the maximum element along each row of the array in the  DIM direction. If  MASK is present, only the elements for which  MASK is  .TRUE. are considered. If more than one element in the array has the maximum value, the location returned is that of the first such element in array element order. If the array has zero size, or all of the elements of  MASK are  .FALSE., then the result is an array of zeroes. Similarly, if  DIM is supplied and all of the elements of  MASK along a given row are zero, the result value for that row is zero. 
Standard:
Fortran 95 and later 
Class:
Transformational function 
Syntax:

RESULT = MAXLOC(ARRAY, DIM [, MASK]) 
RESULT = MAXLOC(ARRAY [, MASK]) 

Arguments:

ARRAYShall be an array of type INTEGER or REAL
DIM(Optional) Shall be a scalar of type INTEGER, with a value between one and the rank of ARRAY, inclusive. It may not be an optional dummy argument. 
MASKShall be an array of type LOGICAL, and conformable with ARRAY

Return value:
If  DIM is absent, the result is a rank-one array with a length equal to the rank of  ARRAY. If  DIM is present, the result is an array with a rank one less than the rank of  ARRAY, and a size corresponding to the size of  ARRAY with the DIM dimension removed. If  DIM is present and  ARRAY has a rank of one, the result is a scalar. In all cases, the result is of default  INTEGER type. 

program main
implicit none
integer ::i,j
integer,dimension(2) ::pos
integer,dimension(2:4,3:6) ::arr

data((arr(i,j),i=2,4),j=3,6)/1,3,4,5,6,7,8,9,10,25,76,89/
pos=maxloc(arr)
write(*,*)'i=',pos(1),'j=',pos(2)
pos=minloc(arr)
write(*,*)'i=',pos(1),'j=',pos(2)


end


程序输出为

i=           3 j=           4
 i=           1 j=           1

输出的最大值的位置为相对于第一个元素的位置,是相对位置,而不是该元素的实际索引

比如第一个元素的位置是(2,3) maxloc对应元素的实际位置需(3,4)+(2,3)-(1,1)=(4,6)

minloc函数也相同





EQUIVALENCE   

equivalence表达式

The EQUIVALENCE statement specifies that two or more variables or arrays in a program unit share the same memory.

EQUIVALENCE (nlist) [, (nlist)] ...

Parameter 

Description 

nlist

List of variable names, array element names, array names, and character substring names separated by commas  


An EQUIVALENCE statement stipulates that the storage sequence of the entities whose names appear in the list nlist must have the same first memory location.

EQUIVALENCE语句规定了名字出现在nlist中的实体的存储序列必须具有相同的首个内存位置


An array name, if present, refers to the first element of the array.

如果数组名出现在参数中,那么它代表数组的第一个元素

program main
implicit none
integer,dimension(2) ::arr
integer ::i
equivalence(i,arr(1))
arr(1)=5
write(*,*) i  !输出5
i=100
write(*,*) arr(1)   !输出100
end

equivalence语句有点像C++的引用,equivalence的参数中的变量指向了内存中的同一个位置,修改其中一个参数,其他参数的值也就跟着改变


获取cpu的时间,可以用于测试某段代码所花的时间

cite from http://gcc.gnu.org/onlinedocs/gcc-4.0.4/gfortran/CPU_005fTIME.html

7.34 CPU_TIME — CPU elapsed time in seconds

Description:
Returns a  REAL value representing the elapsed CPU time in seconds. This is useful for testing segments of code to determine execution time. 
Option:
f95, gnu 
Class:
subroutine 
Syntax:
CPU_TIME(X) 
Arguments:

XThe type shall be REAL with intent out. 

Return value:
None 
Example:
          program test_cpu_time
              real :: start, finish
              call cpu_time(start)
                  ! put code to test here
              call cpu_time(finish)
              print '("Time = ",f6.3," seconds.")',finish-start
          end program test_cpu_time



13. Common blocks -f77 common block

Fortran 77 has no global variables, i.e. variables that are shared among several program units (subroutines). The only way to pass information between subroutines we have seen so far is to use the subroutine parameter list. Sometimes this is inconvenient, e.g., when many subroutines share a large set of parameters. In such cases one can use a common block. This is a way to specify that certain variables should be shared among certain subroutines. But in general, the use of common blocks should be minimized.


Syntax

      common / name / list-of-variables

Example

Suppose you have two parameters alpha and beta that many of your subroutines need. The following example shows how it can be done using common blocks.

      program main
      some declarations
      real alpha, beta
      common /coeff/ alpha, beta

      statements
      stop
      end

      subroutine sub1 (some arguments)
      declarations of arguments
      real alpha, beta
      common /coeff/ alpha, beta

      statements
      return
      end

      subroutine sub2 (some arguments)
      declarations of arguments
      real alpha, beta
      common /coeff/ alpha, beta

      statements
      return
      end
fortran77没有全局变量,在子程序之间传递信息的方法就是通过子程序的参数列表。
common block
当子程序之间共享大集合的变量的时候这就不方便了.这种方式指定特定的变量在特定的子程序之间共享

比如变量alpha与beta需要在不同子程序之间共享,可以定义如下
这里斜杠里面的coeff是common block的名字
  common /coeff/ alpha, beta

  • The common statement should appear together with the variable declarations, before the executable statements.
  • Different common blocks must have different names (just like variables).
  • A variable cannot belong to more than one common block.
  • The variables in a common block do not need to have the same names each place they occur (although it is a good idea to do so), but they must be listed in the same order and have the same type and size.
common语句应该与变量声明放在一起,位于执行语句之前(不然通不过编译)

common block中的变量不需要在每一次出现的时候使用相同的变量名(尽管用相同变量名比较好),但是他们必须被以同样的顺序排列,具有相同的类型与大小

To illustrate this, look at the following continuation of our example:

      subroutine sub3 (some arguments)
      declarations of arguments
      real a, b
      common /coeff/ a, b

      statements
      return
      end

This declaration is equivalent to the previous version that used alpha and beta


the following paragraph cites from f90 standard

5.5.2.1 Common block storage sequence
For each common block, a common block storage sequence is formed as follows:
(1) A storage sequence is formed consisting of the sequence of storage units contained in the storage
sequences (14.6.3.1) of all data objects in the common block object lists for the common block. The
order of the storage sequences is the same as the order of the appearance of the common block
object lists in the scoping unit.
(2) The storage sequence formed in (1) is extended to include all storage units of any storage sequence
associated with it by equivalence association. The sequence may be extended only by adding
storage units beyond the last storage unit. Data objects associated with an entity in a common block
are considered to be in that common block.


common block对象列表中的数据对象在内存中的布局是连续的,顺序与common block对象列表中出现的顺序相同




ibset

ibset将参数i的二进制表示中位置pos上的位设置为1并且返回第一个参数i的值

cite from

http://gcc.gnu.org/onlinedocs/gfortran/IBSET.html

 IBSET — Set bit

Description:

IBSET returns the value of  I with the bit at position  POS set to one. 
Standard:
Fortran 95 and later 
Class:
Elemental function 
Syntax:
RESULT = IBSET(I, POS) 
Arguments:

IThe type shall be INTEGER
POSThe type shall be INTEGER

Return value:

The return value is of type INTEGER and of the same kind as I



ibclr返回第一个参数i的值并将二进制表示中pos位上的位设为0

IBCLR — Clear bit

Description:
IBCLR returns the value of  I with the bit at position  POS set to zero. 
Standard:
Fortran 95 and later 
Class:
Elemental function 
Syntax:
RESULT = IBCLR(I, POS) 
Arguments:

IThe type shall be INTEGER
POSThe type shall be INTEGER

Return value:
The return value is of type  INTEGER and of the same kind as  I


如果参数i的二进制表示中pos位是1,函数返回true

BTEST — Bit test function

Description:
BTEST(I,POS) returns logical  .TRUE. if the bit at  POS in  I is set. The counting of the bits starts at 0. 
Standard:
Fortran 95 and later 
Class:
Elemental function 
Syntax:
RESULT = BTEST(I, POS) 
Arguments:

IThe type shall be INTEGER
POSThe type shall be INTEGER

Return value:
The return value is of type  LOGICAL 


ishft参数中shift>0为左移,shift等于0不移位,shift<0为右移

8.135 ISHFT — Shift bits

Description:
ISHFT returns a value corresponding to  I with all of the bits shifted  SHIFT places. A value of  SHIFT greater than zero corresponds to a left shift, a value of zero corresponds to no shift, and a value less than zero corresponds to a right shift. If the absolute value of  SHIFT is greater than BIT_SIZE(I), the value is undefined. Bits shifted out from the left end or right end are lost; zeros are shifted in from the opposite end. 
Standard:
Fortran 95 and later 
Class:
Elemental function 
Syntax:
RESULT = ISHFT(I, SHIFT) 
Arguments:

IThe type shall be INTEGER
SHIFTThe type shall be INTEGER

Return value:

The return value is of type INTEGER and of the same kind as I.



8.180 MVBITS — Move bits from one integer to another

Description:
Moves  LEN bits from positions  FROMPOS through  FROMPOS+LEN-1 of  FROM to positions  TOPOS through  TOPOS+LEN-1 of  TO. The portion of argument  TO not affected by the movement of bits is unchanged. The values of  FROMPOS+LEN-1 and  TOPOS+LEN-1 must be less than  BIT_SIZE(FROM)
Standard:
Fortran 95 and later 
Class:
Elemental subroutine 
Syntax:
CALL MVBITS(FROM, FROMPOS, LEN, TO, TOPOS) 
Arguments:

FROMThe type shall be INTEGER
FROMPOSThe type shall be INTEGER
LENThe type shall be INTEGER
TOThe type shall be INTEGER, of the same kind as FROM
TOPOSThe type shall be INTEGER


8.35 BIT_SIZE — Bit size inquiry function

Description:
BIT_SIZE(I) returns the number of bits (integer precision plus sign bit) represented by the type of  I. The result of  BIT_SIZE(I) is independent of the actual value of  I
Standard:
Fortran 95 and later 
Class:
Inquiry function 
Syntax:
RESULT = BIT_SIZE(I) 
Arguments:

IThe type shall be INTEGER






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值