# Should elemental functions accept vector arguments?

**URL:** <https://fortran-lang.discourse.group/t/should-elemental-functions-accept-vector-arguments/8430>\
**Category:** Language enhancement\
**Created:** [August 3, 2024, 8:05pm UTC](https://fortran-lang.discourse.group/t/should-elemental-functions-accept-vector-arguments/8430 "2024-08-03T20:05:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aerosayan](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@aerosayan](https://fortran-lang.discourse.group/u/aerosayan)\
**Post date:** [August 3, 2024, 8:05pm UTC](https://fortran-lang.discourse.group/t/should-elemental-functions-accept-vector-arguments/8430/1 "2024-08-03T20:05:40Z")

</div>

Hi everyone,

Elemental functions seem to only accept scalar arguments, and with good reason.

_However, what do you think of also supporting vector arguments?_

In many cases, we have large arrays of vector data that could benefit from elemental procedures.

For example, here is a small code that converts flow variables in a CFD solver from “conservative” form to “primitive” form. These are called state vectors, and have 5 elements each, to store data for density, velocity, pressure, and energy.

It would be useful to do elemental operations on large arrays of this kind of data, though such operations are done very rarely, in my code, so I don’t have any strong opinion on whether such a feature would be useful in Fortran.

_Will such elemental functions that accept vector arguments, be useful to others?_

Thanks

 ![code](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/a/a902460c67ed833ca648489eb214c180c04434eb.jpeg)

---

<div class="post-metadata">

**Author:** ![rwmsu](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@rwmsu](https://fortran-lang.discourse.group/u/rwmsu)\
**Post date:** [August 3, 2024, 8:44pm UTC](https://fortran-lang.discourse.group/t/should-elemental-functions-accept-vector-arguments/8430/2 "2024-08-03T20:44:54Z")

</div>

I’ve encountered this exact same problem before in my CFD codes. The only ways around it are to either embed the csv array in a derived type and then allocate an array of that type that maps to each cell or vertex in your grid or have separate global arrays for each conservation variable. Unfortunately, I think the array of arrays approach you are forced to adopt will not run as fast as just passing a pure array. Both approaches force you to type more characters and/or lines of code than the way you want to do it.

---

<div class="post-metadata">

**Author:** ![wspector](https://avatars.discourse-cdn.com/v4/letter/w/47e85d/32.png) [@wspector](https://fortran-lang.discourse.group/u/wspector)\
**Post date:** [August 4, 2024, 12:54pm UTC](https://fortran-lang.discourse.group/t/should-elemental-functions-accept-vector-arguments/8430/3 "2024-08-04T12:54:16Z")

</div>

This example seems to scream for csv and psv derived data types, e.g.:

```auto
  type csv_t
    real :: rho, urho, vrho, wrho, erho
  end type

  type psv_t
    real :: rho, u, v, w, p
  end type

```

Then write an elemental function to accept a csv\_t item, and return a psv\_t item, e.g.:

```auto
  elemental function convert_csv_to_psv (csv) result (res)
    type(csv_t), intent(in) :: csv
    type(psv_t) :: res

    res%rho = csv%rho
    res%u = csv%urho
    res%v = csv%vrho/csv%rho
    res%w = csv%wrho/csv%rho
    res%p = (1.4-1.0) * (csv%erho-(csv%rho/2.0) * (res%u **2 + res%v** 2 + res%w))

  end function

```

Using associate can simplify access to the individual data elements to help clarify the code:

```auto
  elemental function convert_csv_to_psv (csv) result (res)
    type(csv_t), intent(in) :: csv
    type(psv_t) :: res

    associate (rho=>csv%rho, urho=>csv%urho, vrho=>csv%vrho, wrho=>csv%wrho, erho=>csv%erho)
      res%rho = rho
      res%u = urho/rho
      res%v = vrho/rho
      res%w = wrho/rho
      res%p = (1.4-1.0)*(erho-(rho/2.0)*(res%u **2 + res%v** 2 +res%w**2))
    end associate

  end function

```

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [August 4, 2024, 8:14pm UTC](https://fortran-lang.discourse.group/t/should-elemental-functions-accept-vector-arguments/8430/4 "2024-08-04T20:14:55Z")

</div>

I suggested three years ago that elemental functions be allowed to have a mix of scalar and array arguments. They would only be elemental in the scalar arguments. I think your use case is different.

> [@Partially elemental functions](https://fortran-lang.discourse.group/t/partially-elemental-functions/1216):
>
> Elemental functions are a neat feature of Fortran but are limited by the requirement that all arguments be scalars. Could this be relaxed to allow a mix of scalar and array arguments? A partially elemental function would be elemental for the scalar arguments, which in the caller would be scalars or conformable arrays as they are now. A function such as elemental function moment(power,offset,x) result(y) integer, intent(in) :: power real , intent(in) :: offset real , intent(in) :: x(slight_smilereal …
