(Please don’t lob rocks at me. I love Python.)

  • abbadon420@lemm.ee
    link
    fedilink
    arrow-up
    5
    ·
    3 days ago

    I’ve never played with FORTRAN, but I’ve done some linear algebra with matlab. Matlab was interesting for the native handling if matrices. What makes FORTRAN so good at linear algebra?

    • mkwt@lemmy.world
      link
      fedilink
      arrow-up
      6
      ·
      2 days ago

      Matlab’s syntax for matrices actually derives from Fortran. There’s a lot of flexibility in Fortran’s array features for

      • multidimensional arrays
      • arrays of indeterminate and flexible length
      • vectorized operations on arrays without explicitly writing loops.

      Because Fortran does not have a pointer in the sense of C, the Fortran compiler is free to make several optimization that a C compiler can’t. Compiled Fortran is often faster than C code that does the same thing.

    • lime!@feddit.nu
      link
      fedilink
      English
      arrow-up
      11
      ·
      3 days ago

      the main thing that makes fortran preferable to C is the way it handles arrays and vectors. due to different pointer semantics, they can be laid out more efficiently in memory, meaning less operations need to be done for a given calculation.

      • LeninOnAPrayer@lemm.ee
        link
        fedilink
        English
        arrow-up
        5
        ·
        edit-2
        2 days ago

        Interesting. Is this a fundamental limitation of C or is it just more preferable and easier to use FORTRAN when implementing it?

        Meaning could the same performance be achieved in C but most optimized libraries are already written so why bother? Or basically C can’t achieve the memory optimization at all?

        • lime!@feddit.nu
          link
          fedilink
          English
          arrow-up
          8
          ·
          edit-2
          2 days ago

          you can get the same performance by using the restrict keyword in C.

          basically, C allows pointer aliasing while fortran does not, which means C programs need to be able to handle cases when a value is accessed from multiple locations. fortran does not, so a lot of accesses can be optimized into immediates, or unrolled without guards.

          restrict is a pinky-promise to the compiler that no overlapping takes place, e.g. that a value will only be accessed from one place. it’s basically rust ownership semantics without enforcement.