efficient computation of haversine distance between elements of collectionsWhat is the difference between & and && in Java?Scala: What is the difference between Traversable and Iterable traits in Scala collections?Best way to compute a function on each element with incremental shadowing of element in these collectionEfficient groupwise aggregation on Scala collectionsCalculation Distance Between PointsEfficient way to build collections from other collectionsEfficient Way To Compute Average PriceEfficient distance matrix computation in Java/Scala, bsxfun for java/scalaSpark Scala: Distance between elements of RDDsHow to efficient search elementsCompute Number of Attributes efficiently in flink

Multiple options vs single option UI

Mistake in years of experience in resume?

Creating a chemical industry from a medieval tech level without petroleum

Prove that the countable union of countable sets is also countable

Is there really no use for MD5 anymore?

How exactly does Hawking radiation decrease the mass of black holes?

What is the best way to deal with NPC-NPC combat?

Negative Resistance

Why do games have consumables?

How to not starve gigantic beasts

Magical attacks and overcoming damage resistance

A faster way to compute the largest prime factor

Drawing a german abacus as in the books of Adam Ries

Crossed out red box fitting tightly around image

Combinatorics problem, right solution?

How do I reattach a shelf to the wall when it ripped out of the wall?

How do I deal with a coworker that keeps asking to make small superficial changes to a report, and it is seriously triggering my anxiety?

Retract an already submitted recommendation letter (written for an undergrad student)

Why doesn't the standard consider a template constructor as a copy constructor?

What *exactly* is electrical current, voltage, and resistance?

What makes accurate emulation of old systems a difficult task?

What to do with someone that cheated their way through university and a PhD program?

Is there any pythonic way to find average of specific tuple elements in array?

Will I lose my paid in full property



efficient computation of haversine distance between elements of collections


What is the difference between & and && in Java?Scala: What is the difference between Traversable and Iterable traits in Scala collections?Best way to compute a function on each element with incremental shadowing of element in these collectionEfficient groupwise aggregation on Scala collectionsCalculation Distance Between PointsEfficient way to build collections from other collectionsEfficient Way To Compute Average PriceEfficient distance matrix computation in Java/Scala, bsxfun for java/scalaSpark Scala: Distance between elements of RDDsHow to efficient search elementsCompute Number of Attributes efficiently in flink






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have two collections. Each collection is comprised of a collection containing a latitude, longitude, and epoch.



val arr1= Seq(Seq(34.464, -115.341,1486220267.0), Seq(34.473, 
-115.452,1486227821.0), Seq(35.572, -116.945,1486217300.0),
Seq(37.843, -115.874,1486348520.0),Seq(35.874, -115.014,1486349803.0),
Seq(34.345, -116,924, 1486342752.0) )

val arr2= Seq(Seq(35.573, -116.945,1486217300.0 ),Seq(34.853,
-114.983,1486347321.0 ) )


I want to determine how many times the two arrays are within .5 miles and have the same epoch. I have two functions



def haversineDistance_single(pointA: (Double, Double), pointB: (Double, Double)): Double = 
val deltaLat = math.toRadians(pointB._1 - pointA._1)
val deltaLong = math.toRadians(pointB._2 - pointA._2)
val a = math.pow(math.sin(deltaLat / 2), 2) + math.cos(math.toRadians(pointA._1)) * math.cos(math.toRadians(pointB._1)) * math.pow(math.sin(deltaLong / 2), 2)
val greatCircleDistance = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
3958.761 * greatCircleDistance


def location_time(col_2:Seq[Seq[Double]], col_1:Seq[Seq[Double]]): Int=
val arr=col_1.map(x=> col_2.filter(y=> (haversineDistance_single((y(0), y(1)), (x(0),x(1)))<=.5) &

(math.abs(y(2)-x(2))<=0)).flatten).filter(x=> x.length>0)
arr.length



location_time(arr1,arr2) =1


My actual collections are very large, is there a more efficient way than my location_time function to compute this.










share|improve this question






























    1















    I have two collections. Each collection is comprised of a collection containing a latitude, longitude, and epoch.



    val arr1= Seq(Seq(34.464, -115.341,1486220267.0), Seq(34.473, 
    -115.452,1486227821.0), Seq(35.572, -116.945,1486217300.0),
    Seq(37.843, -115.874,1486348520.0),Seq(35.874, -115.014,1486349803.0),
    Seq(34.345, -116,924, 1486342752.0) )

    val arr2= Seq(Seq(35.573, -116.945,1486217300.0 ),Seq(34.853,
    -114.983,1486347321.0 ) )


    I want to determine how many times the two arrays are within .5 miles and have the same epoch. I have two functions



    def haversineDistance_single(pointA: (Double, Double), pointB: (Double, Double)): Double = 
    val deltaLat = math.toRadians(pointB._1 - pointA._1)
    val deltaLong = math.toRadians(pointB._2 - pointA._2)
    val a = math.pow(math.sin(deltaLat / 2), 2) + math.cos(math.toRadians(pointA._1)) * math.cos(math.toRadians(pointB._1)) * math.pow(math.sin(deltaLong / 2), 2)
    val greatCircleDistance = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
    3958.761 * greatCircleDistance


    def location_time(col_2:Seq[Seq[Double]], col_1:Seq[Seq[Double]]): Int=
    val arr=col_1.map(x=> col_2.filter(y=> (haversineDistance_single((y(0), y(1)), (x(0),x(1)))<=.5) &

    (math.abs(y(2)-x(2))<=0)).flatten).filter(x=> x.length>0)
    arr.length



    location_time(arr1,arr2) =1


    My actual collections are very large, is there a more efficient way than my location_time function to compute this.










    share|improve this question


























      1












      1








      1








      I have two collections. Each collection is comprised of a collection containing a latitude, longitude, and epoch.



      val arr1= Seq(Seq(34.464, -115.341,1486220267.0), Seq(34.473, 
      -115.452,1486227821.0), Seq(35.572, -116.945,1486217300.0),
      Seq(37.843, -115.874,1486348520.0),Seq(35.874, -115.014,1486349803.0),
      Seq(34.345, -116,924, 1486342752.0) )

      val arr2= Seq(Seq(35.573, -116.945,1486217300.0 ),Seq(34.853,
      -114.983,1486347321.0 ) )


      I want to determine how many times the two arrays are within .5 miles and have the same epoch. I have two functions



      def haversineDistance_single(pointA: (Double, Double), pointB: (Double, Double)): Double = 
      val deltaLat = math.toRadians(pointB._1 - pointA._1)
      val deltaLong = math.toRadians(pointB._2 - pointA._2)
      val a = math.pow(math.sin(deltaLat / 2), 2) + math.cos(math.toRadians(pointA._1)) * math.cos(math.toRadians(pointB._1)) * math.pow(math.sin(deltaLong / 2), 2)
      val greatCircleDistance = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
      3958.761 * greatCircleDistance


      def location_time(col_2:Seq[Seq[Double]], col_1:Seq[Seq[Double]]): Int=
      val arr=col_1.map(x=> col_2.filter(y=> (haversineDistance_single((y(0), y(1)), (x(0),x(1)))<=.5) &

      (math.abs(y(2)-x(2))<=0)).flatten).filter(x=> x.length>0)
      arr.length



      location_time(arr1,arr2) =1


      My actual collections are very large, is there a more efficient way than my location_time function to compute this.










      share|improve this question
















      I have two collections. Each collection is comprised of a collection containing a latitude, longitude, and epoch.



      val arr1= Seq(Seq(34.464, -115.341,1486220267.0), Seq(34.473, 
      -115.452,1486227821.0), Seq(35.572, -116.945,1486217300.0),
      Seq(37.843, -115.874,1486348520.0),Seq(35.874, -115.014,1486349803.0),
      Seq(34.345, -116,924, 1486342752.0) )

      val arr2= Seq(Seq(35.573, -116.945,1486217300.0 ),Seq(34.853,
      -114.983,1486347321.0 ) )


      I want to determine how many times the two arrays are within .5 miles and have the same epoch. I have two functions



      def haversineDistance_single(pointA: (Double, Double), pointB: (Double, Double)): Double = 
      val deltaLat = math.toRadians(pointB._1 - pointA._1)
      val deltaLong = math.toRadians(pointB._2 - pointA._2)
      val a = math.pow(math.sin(deltaLat / 2), 2) + math.cos(math.toRadians(pointA._1)) * math.cos(math.toRadians(pointB._1)) * math.pow(math.sin(deltaLong / 2), 2)
      val greatCircleDistance = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
      3958.761 * greatCircleDistance


      def location_time(col_2:Seq[Seq[Double]], col_1:Seq[Seq[Double]]): Int=
      val arr=col_1.map(x=> col_2.filter(y=> (haversineDistance_single((y(0), y(1)), (x(0),x(1)))<=.5) &

      (math.abs(y(2)-x(2))<=0)).flatten).filter(x=> x.length>0)
      arr.length



      location_time(arr1,arr2) =1


      My actual collections are very large, is there a more efficient way than my location_time function to compute this.







      scala






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 22 at 17:24







      mikeL

















      asked Mar 22 at 16:33









      mikeLmikeL

      4392514




      4392514






















          1 Answer
          1






          active

          oldest

          votes


















          2














          I would consider revising location_time from:



          def location_time(col_mobile: Seq[Seq[Double]], col_laptop: Seq[Seq[Double]]): Int = 
          val arr = col_laptop.map( x => col_mobile.filter( y =>
          (haversineDistance_single((y(0), y(1)), (x(0), x(1))) <= .5) & (math.abs(y(2) - x(2)) <= 0)
          ).flatten
          ).filter(x => x.length > 0)

          arr.length



          to:



          def location_time(col_mobile: Seq[Seq[Double]], col_laptop: Seq[Seq[Double]]): Int = 
          val arr = col_laptop.flatMap( x => col_mobile.filter( y =>
          ((math.abs(y(2) - x(2)) <= 0 && haversineDistance_single((y(0), y(1)), (x(0), x(1))) <= .5))
          )
          )

          arr.length



          Changes made:




          1. Revised col_mobile.filter(y => ...) from:



            filter(_ => costlyCond1 & lessCostlyCond2)


            to:



            filter(_ => lessCostlyCond2 && costlyCond1)


            Assuming haversineDistance_single is more costly to run than math.abs, replacing & with && (see difference between & versus &&) and testing math.abs first might help the filtering performance.




          2. Simplified map/filter/flatten/filter using flatMap, replacing:



            col_laptop.map(x => col_mobile.filter(y => ...).flatten).filter(_.length > 0)


            with:



            col_laptop.flatMap( x => col_mobile.filter( y => ... ))


          In case you have access to, say, an Apache Spark cluster, consider converting your collections (if they're really large) to RDDs to compute using transformations similar to the above.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304053%2fefficient-computation-of-haversine-distance-between-elements-of-collections%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            I would consider revising location_time from:



            def location_time(col_mobile: Seq[Seq[Double]], col_laptop: Seq[Seq[Double]]): Int = 
            val arr = col_laptop.map( x => col_mobile.filter( y =>
            (haversineDistance_single((y(0), y(1)), (x(0), x(1))) <= .5) & (math.abs(y(2) - x(2)) <= 0)
            ).flatten
            ).filter(x => x.length > 0)

            arr.length



            to:



            def location_time(col_mobile: Seq[Seq[Double]], col_laptop: Seq[Seq[Double]]): Int = 
            val arr = col_laptop.flatMap( x => col_mobile.filter( y =>
            ((math.abs(y(2) - x(2)) <= 0 && haversineDistance_single((y(0), y(1)), (x(0), x(1))) <= .5))
            )
            )

            arr.length



            Changes made:




            1. Revised col_mobile.filter(y => ...) from:



              filter(_ => costlyCond1 & lessCostlyCond2)


              to:



              filter(_ => lessCostlyCond2 && costlyCond1)


              Assuming haversineDistance_single is more costly to run than math.abs, replacing & with && (see difference between & versus &&) and testing math.abs first might help the filtering performance.




            2. Simplified map/filter/flatten/filter using flatMap, replacing:



              col_laptop.map(x => col_mobile.filter(y => ...).flatten).filter(_.length > 0)


              with:



              col_laptop.flatMap( x => col_mobile.filter( y => ... ))


            In case you have access to, say, an Apache Spark cluster, consider converting your collections (if they're really large) to RDDs to compute using transformations similar to the above.






            share|improve this answer



























              2














              I would consider revising location_time from:



              def location_time(col_mobile: Seq[Seq[Double]], col_laptop: Seq[Seq[Double]]): Int = 
              val arr = col_laptop.map( x => col_mobile.filter( y =>
              (haversineDistance_single((y(0), y(1)), (x(0), x(1))) <= .5) & (math.abs(y(2) - x(2)) <= 0)
              ).flatten
              ).filter(x => x.length > 0)

              arr.length



              to:



              def location_time(col_mobile: Seq[Seq[Double]], col_laptop: Seq[Seq[Double]]): Int = 
              val arr = col_laptop.flatMap( x => col_mobile.filter( y =>
              ((math.abs(y(2) - x(2)) <= 0 && haversineDistance_single((y(0), y(1)), (x(0), x(1))) <= .5))
              )
              )

              arr.length



              Changes made:




              1. Revised col_mobile.filter(y => ...) from:



                filter(_ => costlyCond1 & lessCostlyCond2)


                to:



                filter(_ => lessCostlyCond2 && costlyCond1)


                Assuming haversineDistance_single is more costly to run than math.abs, replacing & with && (see difference between & versus &&) and testing math.abs first might help the filtering performance.




              2. Simplified map/filter/flatten/filter using flatMap, replacing:



                col_laptop.map(x => col_mobile.filter(y => ...).flatten).filter(_.length > 0)


                with:



                col_laptop.flatMap( x => col_mobile.filter( y => ... ))


              In case you have access to, say, an Apache Spark cluster, consider converting your collections (if they're really large) to RDDs to compute using transformations similar to the above.






              share|improve this answer

























                2












                2








                2







                I would consider revising location_time from:



                def location_time(col_mobile: Seq[Seq[Double]], col_laptop: Seq[Seq[Double]]): Int = 
                val arr = col_laptop.map( x => col_mobile.filter( y =>
                (haversineDistance_single((y(0), y(1)), (x(0), x(1))) <= .5) & (math.abs(y(2) - x(2)) <= 0)
                ).flatten
                ).filter(x => x.length > 0)

                arr.length



                to:



                def location_time(col_mobile: Seq[Seq[Double]], col_laptop: Seq[Seq[Double]]): Int = 
                val arr = col_laptop.flatMap( x => col_mobile.filter( y =>
                ((math.abs(y(2) - x(2)) <= 0 && haversineDistance_single((y(0), y(1)), (x(0), x(1))) <= .5))
                )
                )

                arr.length



                Changes made:




                1. Revised col_mobile.filter(y => ...) from:



                  filter(_ => costlyCond1 & lessCostlyCond2)


                  to:



                  filter(_ => lessCostlyCond2 && costlyCond1)


                  Assuming haversineDistance_single is more costly to run than math.abs, replacing & with && (see difference between & versus &&) and testing math.abs first might help the filtering performance.




                2. Simplified map/filter/flatten/filter using flatMap, replacing:



                  col_laptop.map(x => col_mobile.filter(y => ...).flatten).filter(_.length > 0)


                  with:



                  col_laptop.flatMap( x => col_mobile.filter( y => ... ))


                In case you have access to, say, an Apache Spark cluster, consider converting your collections (if they're really large) to RDDs to compute using transformations similar to the above.






                share|improve this answer













                I would consider revising location_time from:



                def location_time(col_mobile: Seq[Seq[Double]], col_laptop: Seq[Seq[Double]]): Int = 
                val arr = col_laptop.map( x => col_mobile.filter( y =>
                (haversineDistance_single((y(0), y(1)), (x(0), x(1))) <= .5) & (math.abs(y(2) - x(2)) <= 0)
                ).flatten
                ).filter(x => x.length > 0)

                arr.length



                to:



                def location_time(col_mobile: Seq[Seq[Double]], col_laptop: Seq[Seq[Double]]): Int = 
                val arr = col_laptop.flatMap( x => col_mobile.filter( y =>
                ((math.abs(y(2) - x(2)) <= 0 && haversineDistance_single((y(0), y(1)), (x(0), x(1))) <= .5))
                )
                )

                arr.length



                Changes made:




                1. Revised col_mobile.filter(y => ...) from:



                  filter(_ => costlyCond1 & lessCostlyCond2)


                  to:



                  filter(_ => lessCostlyCond2 && costlyCond1)


                  Assuming haversineDistance_single is more costly to run than math.abs, replacing & with && (see difference between & versus &&) and testing math.abs first might help the filtering performance.




                2. Simplified map/filter/flatten/filter using flatMap, replacing:



                  col_laptop.map(x => col_mobile.filter(y => ...).flatten).filter(_.length > 0)


                  with:



                  col_laptop.flatMap( x => col_mobile.filter( y => ... ))


                In case you have access to, say, an Apache Spark cluster, consider converting your collections (if they're really large) to RDDs to compute using transformations similar to the above.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 22 at 18:08









                Leo CLeo C

                12.5k2820




                12.5k2820





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304053%2fefficient-computation-of-haversine-distance-between-elements-of-collections%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현