Do these 2 lines of Fortran represent a Fortran macro? - fortran

I am converting some Fortran code to C and I don't understand what is going on here.
This code came from TOMS, Transactions on Mathematical Software, so I don't believe this is an errant line of code.
ZCOS and A have no other definitions in the file other than what I am showing here. Is this a Fortran technique for defining ZCOS as COS, and if so, what does it accomplish?
REAL ZCOS
ZCOS(A) = COS(A)
C(I) = ONE / (TWO * ZCOS ( C(I) * PI / DBLE(N+N) ))

This is a statement function, explained many times on SO. ZCOS is a real function and is defined in the first 2 lines.
The first line define the type of ZCOS.
The second line defines the function itself.
The third line could be an array access or another statement function. It cannot be decided without context.
Remark: Often, ZCOS is a specific functions for the COS generic, taking a double complex argument. It is a non-standard extension and your code does not use this intrinsic.

Related

Is it possible to suppress the top level in a reference to a derived type?

This is hard to describe in words but an example should make it clear. Let's say I have a variable of a derived type, with the following components.
x%length
x%width
Is there any automatic way to refer to these without the top level? In other words to refer to them as simply
length
width
Of course, I could first do
length => x%length
width => x%width
for ALL individual components of the derived type. But my use case involves thousands of variables, so I'd prefer not to do it that way.
As an example from another language, python will essentially allow this suppression with:
from x import *
There is no such a functionality in fortran as far as I know, at least in the implementations that I have at hand. Beside that, the objectives of my post is to make some other thinks clear.
The python from x import * is the equivalent of use x in fortran. I am not very pythonic, but I do not think that you can import member of a class directly. So, that works as long as x is a pyton module, not a python class to my limited knowledge. use x will also works as long as x is a fortran module.
One of the programming language that I know of and that implements the feature that you are after is pascal. There is this handy construct with that allows you to do that.
with x do
begin
lenght ....
width ....
end
Indeed, it is very helpful in that it allows you to strip a part of the object name and get directly to fields. I loved it when I was using pascal, but it's been a long time.
Delphi certainly allows that too.
How about the Fortran 2003 associate construct? This will, in a sense, manage for you the pointer assignments that you listed:
Program test
Type :: t
Integer :: length
Integer :: width
End Type
Type (t) :: x = t(42, 43)
Associate (length=>x%length, width=>x%width)
Print *, length, width
End Associate
End Program
Quoting from Fortran 2003 (e.g., at http://www.j3-fortran.org/doc/year/04/04-007.pdf): "The ASSOCIATE construct associates named entities with expressions or variables during the execution of its block."
The December 2015 ACM Fortran Forum "Compiler Support" article lists the associate construct as being fully supported by Cray, IBM, Intel and NAG and partially supported by gfortran.
I don't think there is any way to simplify this though if you have many type components to alias in this way.

How to make input part of my code?

So I have this maths project where I have to write a program which calculates definite integral of a given function within the given boundaries. I've done this using C++ and CodeBlocks, but now I would like to try and make it possible to input function using cmd when I run my code in CodeBlocks, just like I input boundaries, so I don't have to edit this line of code every time I want to run it for different function. I realised that this would require actually using then this input ( e.g. "sqrt(pow(x,2)-1)" ) as part of the code when entered, and I really don't know how to do this or if it is possible at all, so any help is welcome.
This the part of the code which handles function:
double Formula(double x)
{
double a;
a = sqrt(x);
return a;
}
If you want to evaluate expression like "sqrt(pow(x,2)-1)", you have to:
parse the string and generate an AST (Abstract syntax tree) which describes the operations to execute
use an evaluation function on the AST
For example, if you have "sqrt(pow(x,2)-1)" in input, the AST could be represented like this:
function - sqrt
function - substract
function - pow
variable - x
integer - 2
integer - -1
You have to define the structures which will be used to represent your AST.
Then, to parse the query string you have 2 choices:
parse it yourself, count the parentheses etc...
use a tool to generate the parser: yacc + lex or under linux bison + flex. These tools require time to be used to them.
If you have just a little project to do, you may have to try to parse the input yourself to generate the AST.
If the project is a compilation project, you should use bison + flex, they are exactly made for that (but require time to be used to ! ).
Alternatively, integrate with a scripting language, make it do the function parsing and evaluation. It will be considerably slower though.
JavaScript interpreters are all over the place. Python is fairly popular, too. Some people like Lua.

fortran format specifier for complex number

Can I specify a format specifier for a complex number in fortran? I have a simple program.
program complx1
implicit none
complex :: var1
var1 = (10,20)
write (*,*) var1
write (*,'(F0.0)') var1
write (*,'(F0.0,A,F0.0)') real(var1), ' + i ' , aimag(var1)
end program complx1
Output:
( 10.0000000 , 20.0000000 )
10.
20.
10. + i 20.
I wanted to use inbuilt format for a+bi with some format specifier, instead of one did manually (second last line of program). Obviously F0.0 did not work. Any ideas?
EDIT:
I don't think this is a duplicate of post: writing complex matrix in fortran, which says to use REAL and AIMAG functions. I already used those functions and wondering whether there is an inbuilt format that can do the work.
An addendum to #francescalus' existing, and mostly satisfactory, answer. A format string such as
fmt = '(F0.0,SP,F0.0,"i")'
should result in a complex number being displayed with the correct sign between real and imaginary parts; no need to fiddle around with strings to get a plus sign in there.
There is no distinct complex edit descriptor. In Fortran 2008, 10.7.2.3.6 we see
A complex datum consists of a pair of separate real data. The editing of a scalar datum of complex type is specified by two edit descriptors each of which specifies the editing of real data.
In your second example, which you say "did not work", you see this formatting in action. Because the format had only one descriptor with no repeat count the values are output in distinct records (format reversion).
The first of your three cases is a very special one: it uses list-directed output. The rules for the output are
Complex constants are enclosed in parentheses with a separator between the real and imaginary parts
There is another useful part of the first rule mentioned:
Control and character string edit descriptors may be processed between the edit descriptor for the real part and the edit descriptor for the imaginary part.
You could happily adapt your second attempt, as we note that your "not working" wasn't because of the use of the complex variable itself (rather than the real and imaginary components)
write (*, '(F0.0,"+i",F0.0)') var1
This, though, isn't right when you have potentially negative complex part. You'll need to change the sign in the middle. This is possible, using a character variable format (rather than literal) with a conditional, but it perhaps isn't worth the effort. See another answer for details of another approach, similar to your third option but more robust.
Another option is to write a function which returns a correctly written character representation of your complex variable. That's like your third option. It is also the least messy approach when you want to write out many complex variables.
Finally, if you do have to worry about negative complex parts but want a simple specification of the variable list, there is the truly ugly
write(*,'(F0.0,"+i*(",F0.0,")")') var1
or the imaginative
character(19) fmt
fmt = '(F0.0,"+",F0.0,"i")'
fmt(8:8) = MERGE('+',' ',var1%im.gt.0)
write(*,fmt) var1
or the even better use of the SP control edit descriptor as given by High Performance Mark's answer which temporarily (for the duration of the output statement) sets the sign mode of the transfer to PLUS which forces the printing of the otherwise optional "+" sign. [Alternatively, this can be set for the connection itself in an open with the sign='plus' specifier.]
All this is because the simple answer is: no, there is no in-built complex edit descriptor.

Sympy: Howto to rewrite erf function

I have an expression in SymPy that involves the normal cumulative function, N(x) which is directly linked to the error function through the equation N(x)=0.5*erf(x/sqrt(2)) + 0.5.
When I use the Normal(0,1).cdf(x) function of SymPy, it is written using the error function. So, when I output latex string of some (complicated) expression, the seem more complicated when using erf (instead of N(x), it outputs the equation mentionned obove). I tried to define a symbol N=0.5*erf(x/sqrt(2)) + 0.5 and tried the command 'rewrite' the rewrite my expression in terms of N, but 'rewrite' seems to work only with internally defined functions.
Does any bodu know how to rewrite erf(some_expression) in terms of N(some_expression), given that I don't know some_expression in advance (can't use subs) ?
Thanks in advance
I take it from your question that you are using Normal from sympy.statistics. You should move to sympy.stats. sympy.statistics has been deprecated for some time, and will be removed in the next version of SymPy.
To answer your question more directly, you can replace functions with functions using replace, like expr.replace(erf, lambda x: (N(x) - 0.5)/0.5).
The problem here is that there is no function N. I would expect this to be done better in sympy.stats, where the distributions are represented symbolically. However, I didn't find a way to do it. I opened https://github.com/sympy/sympy/issues/7819 for this.

Star * symbol found on second line of Fortran function declaration

When examining some legacy Fortran code, I've found a subroutine declaration that is the following:
SUBROUTINE CLIP2G (fcut,TIME,NUMS,NUMG,CLIPG,CLIPGL,CLIPGR,
* MODE,PHZ)
What does the * signify in this context? Does the star * mean that the subroutine declaration spans two lines rather than one line?
This is line continuation mark. It marks that this line is a continuation of the previous one. It may be any Fortran recognized character, but it must be placed in column 6. More thorough discussion of line continuations in fixed and free Fotran format is here.