Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assign linspaced values to an array slice throws an error in fortran #1212

Open
rakati opened this issue Oct 8, 2022 · 4 comments · May be fixed by #1215
Open

assign linspaced values to an array slice throws an error in fortran #1212

rakati opened this issue Oct 8, 2022 · 4 comments · May be fixed by #1215
Assignees
Labels

Comments

@rakati
Copy link
Member

rakati commented Oct 8, 2022

Describe the bug
assigning linspaced values to an array is working fine, but to a slice of an array throws an error in Fortran.
To Reproduce

def func(n:int):
  p = n//3
  arr = np.zeros(n)
  arr[p-1:n-p] = np.linspace(0, 1, p)
  return arr

but this succeeds:

def func(n:int):
  p = n//3
  arr = np.zeros(n)
  tmp_arr = np.linspace(0, 1, p)
  arr[p-1:n-p] = tmp_arr
  return arr

The error message:

......
......
File "/home/rakati/pyccel_dev/pyccel/pyccel/codegen/printing/fcode.py", line 2846, in _print_IndexedElement
    allow_negative_indexes = base.allows_negative_indexes
AttributeError: 'IndexedElement' object has no attribute 'allows_negative_indexes'

A clear and concise description of what you expected to happen.

Language
Fortran

@rakati rakati added the bug label Oct 8, 2022
@nhamidn
Copy link
Member

nhamidn commented Oct 9, 2022

your code give me an error in python :

ValueError: could not broadcast input array from shape (X,) into shape (Y,)

different shapes

@nhamidn
Copy link
Member

nhamidn commented Oct 10, 2022

i have added allows_negative_indexes property to IndexedElement class with a default value of False just to test, i found another problem with Linspace function.

The problem is we always correct the last element of the generated array to match the end value, in fortran when we try to correct the last element using arr(0_i64:p - 1_i64)(p - 1_i64) = 1.0_f64 of a slice we get an error Error: Unclassifiable statement at (1)

this python function :

def func(n:int):
    p = n//3
    print(p)
    arr = np.zeros(n)
    arr[0:p] = np.linspace(0, 1, p)
    return arr

translated to this fortran function:

module linpace

  use, intrinsic :: ISO_C_Binding, only : f64 => C_DOUBLE , i64 => &
        C_INT64_T
  implicit none

  contains

  !........................................
  subroutine func(n, arr) 

    implicit none

    real(f64), allocatable, intent(out) :: arr(:)
    integer(i64), value :: n
    integer(i64) :: p
    integer(i64) :: linspace_index

    p = FLOOR(n/3.0_f64,i64)
    write(*, '(I0)', advance="yes") p
    allocate(arr(0:n - 1_i64))
    arr = 0.0_f64
    arr(0_i64:p - 1_i64) = [((0_i64 + linspace_index*Real((1_i64 - 0_i64 &
          ), f64) / Real((p - 1_i64), f64)), linspace_index = 0_i64,p - &
          1_i64)]
    arr(0_i64:p - 1_i64)(p - 1_i64) = 1.0_f64
    return

  end subroutine func
  !........................................

end module linpace

this case is not handled in _print_NumpyLinspace yet

@nhamidn nhamidn self-assigned this Oct 10, 2022
@EmilyBourne
Copy link
Member

Duplicate of #1218

@EmilyBourne EmilyBourne marked this as a duplicate of #1218 Oct 30, 2022
@yguclu yguclu linked a pull request Mar 3, 2023 that will close this issue
3 tasks
@EmilyBourne EmilyBourne added the blocked Cannot be solved/merged until something else is fixed label Nov 23, 2023
@EmilyBourne EmilyBourne removed the blocked Cannot be solved/merged until something else is fixed label May 13, 2024
@EmilyBourne
Copy link
Member

This PR is not quite a duplicate. On the devel branch the following code is generated:

module issue_1212


  use, intrinsic :: ISO_C_Binding, only : i64 => C_INT64_T , f64 => &
        C_DOUBLE
  implicit none
    
  contains

  !........................................
  subroutine func(n, arr) 

    implicit none

    real(f64), allocatable, intent(out) :: arr(:)
    integer(i64), value :: n
    integer(i64) :: p
    integer(i64) :: linspace_index

    p = FLOOR(n/3.0_f64,i64)
    allocate(arr(0:n - 1_i64))
    arr = 0.0_f64
    arr(p - 1_i64:n - p - 1_i64) = [((0_i64 + linspace_index*Real((1_i64 &
          - 0_i64), f64) / Real((p - 1_i64), f64)), linspace_index = &
          0_i64,p - 1_i64)]
    arr(p - 1_i64, p - 1_i64:n - p - 1_i64) = 1.0_f64
    return

  end subroutine func
  !........................................

end module issue_1212

which generates the compiler error:

/home/emily/Code/test/tmp/__pyccel__/issue_1212.f90:26:7:

   26 |     arr(p - 1_i64, p - 1_i64:n - p - 1_i64) = 1.0_f64
      |       1
Error: Rank mismatch in array reference at (1) (2/1)

This code is wrong as it corresponds to arr[p-1:n-p][p-1]. An error should be raised in IndexedElement if the base is a slice as this behaviour is not supported in the semantic parser either. The code should be fixed to index correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants