performance – How to make my code with for/if loop more efficient in R

My code is structured so that I create an ellipse through my first three points of data. Then I go through each next row, and if the point falls outside the ellipse, I record the minimum distance between the point and ellipse. These points have datetime associated with it so I can only keep points 10 minutes old in my ellipse (so it doesn’t get too big). The code works great until I have to process 100K+ of observations. Then the process time goes from minutes to hours. My question is, is there any part of my for loop portion of code that I can restructure to cut process time? I vectorized what I could (I think), and I know taking conditions out of for loop helps but now sure how to do it. Code below for reference. Any help is greatly appreciated.

library(sp)
library(cluster)
#create new empty column in dataframe
df$Distances <- NA

#get first three points and fit an ellipse around it
FlashPoints <- cbind(df$Lat(1:3),df$Long(1:3))
EllipseShape <- ellipsoidhull(FlashPoints)
EllipseShape

#get set of points that represent ellipse
EllipsePoints <- predict(EllipseShape)
EllipseGroup <- cbind(EllipsePoints(,2),EllipsePoints(,1))

#function to check if the next flashpoint (xp, yp) belongs to the ellipse with parameters a,b,... with tolerance eps
onEllipse <- function (xp, yp, a, b, x0, y0, alpha, eps=1e-3) {
  return(abs((cos(alpha)*(xp-x0)+sin(alpha)*(yp-y0))^2/a^2+(sin(alpha)*(xp-x0)-cos(alpha)*(yp-y0))^2/b^2 - 1) <= eps)
} 

#function to check if the point (xp, yp) is inside the ellipse with parameters a,b,...
insideEllipse <- function (xp, yp, a, b, x0, y0, alpha) {
  return((cos(alpha)*(xp-x0)+sin(alpha)*(yp-y0))^2/a^2+(sin(alpha)*(xp-x0)-cos(alpha)*(yp-y0))^2/b^2 <= 1)
}

#loop from row 4 to last row
for (i in 4:nrow(df)){

#get location of next point
nextFlash <- cbind(df$Long(i),df$Lat(i))

#Establish/Re-establish parameters
xp <- df$Lat(i)
yp <- df$Long(i)
x0 <- EllipseShape$loc(1) # centroid locations
y0 <- EllipseShape$loc(2)
eg <- eigen(EllipseShape$cov)
axes <- sqrt(eg$values)
alpha <- atan(eg$vectors(1,1)/eg$vectors(2,1)) # angle of major axis with x axis
a <- sqrt(EllipseShape$d2) * axes(1)  # major axis length
b <- sqrt(EllipseShape$d2) * axes(2)  # minor axis length


#Check to see if next point is outside the ellipse, then get distance. If it is, add to distance list
if((insideEllipse(xp, yp, a, b, x0, y0, alpha)==FALSE) && onEllipse(xp, yp, a, b, x0, y0, alpha)==FALSE){
  nextDist <- min(spDistsN1(EllipseGroup, nextFlash, longlat = TRUE))
  df$Distances(i) <- nextDist
    
#Omit points 10 minutes old from group
  movingGroup <- subset(df(1:i,), difftime(df$DateTime(1:i),df$DateTime(i),units = "mins")<10)
  movingTstorm
  FlashPoints <- cbind(movingGroup$Lat(1:i),movingGroup$Long(1:i))
  FlashPoints
  
  #Fit an ellipse around flashpoints
  EllipseShape <- ellipsoidhull(FlashPoints)
  EllipseShape
  
  #Get set of points that make an ellipse
  EllipsePoints <- predict(EllipseShape)
  EllipseGroup <- cbind(EllipsePoints(,2),EllipsePoints(,1))
} 
  
next(i)
}
}
```

recursion – Calling recursive method in a loop – Backtracking

I’m confused about a matter that I’ve been unable to figure out. I’m doing some leetcode problems. In backtracking problems, sometimes we use loop within our recursive method to call the recursion but other times, I see solutions where in backtracking they’re not using loop to call the recursive method.

For example:
The question: Partition Sum Equal K

I solved the question using the following code:

private boolean findCombinations(int() nums, int index, int sum, int target, HashMap<String, Boolean> map) {
        if(index >= nums.length) {
            if(sum * 2 == target)
                return true;

            return false;
        }
        
        if(sum * 2 > target)
            return false;
        
        String key = index+""+sum;
        
        if(map.containsKey(key))
            return map.get(key);
        
        boolean foundCombination = false;

        //looping to find all combinations that lead to desired result
        for(int i=index; i<nums.length; i++) {
            
            foundCombination = findCombinations(nums, i+1, sum+nums(i), target, map);
            
            if(foundCombination)
                break;
        }
        
        map.put(key, foundCombination);
        
        return foundCombination;
    }

However I found solutions where they’re not using the loop rather calling the method just twice like the following:

private boolean findCombinations(int() nums, int index, int sum, int target, HashMap<String, Boolean> map) {
            if(index >= nums.length) {
                if(sum * 2 == target)
                    return true;
    
                return false;
            }
            
            if(sum * 2 > target)
                return false;
            
            String key = index+""+sum;
            
            if(map.containsKey(key))
                return map.get(key);
            
            boolean foundCombination = false;

          
            foundCombination = findCombinations(nums, i+1, sum+nums(i), target, map) || findCombinations(nums, i+1, sum, target, map);
            
            map.put(key, foundCombination);
            
            return foundCombination;
        }

I realized that calling the method by making 2 calls will be sufficient but then in other questions like Combination sum, we’re still using the loop over the recursive method to find all the desired result.

Combination Sum code:

private void findCombinations(int() nums, int index, int target, int sum, List<Integer> comb, List<List<Integer>> result) {
        if(sum == target) {
            result.add(new ArrayList<>(comb));
            return;
        }
        
        if(index >= nums.length || sum > target) {
            return;
        }
        
        for(int i=index; i<nums.length; i++) {
            if(i != index && nums(i-1) == nums(i))
                continue;
            
            comb.add(nums(i));
            
            findCombinations(nums, i+1, target, sum+nums(i), comb, result);
            
            comb.remove(comb.size()-1);
        }
    }

Can someone help me understand under what circumstances, it’s better to use loop over recursive method and when not to use it?

external sd – Cant’ mount loop device

I’m trying to mount loop.img (formatted as ext4) from external sdcard to /data/data/com.termux/home/ubuntu20-fs/
but always throw invalid argument, I already tried using busybox too.

mount -t ext4 -o loop /dev/block/loop4 /data/data/com.termux/files/home/ubuntu20-fs/
mount: '/dev/block/loop4'->'/data/data/com.termux/files/home/ubuntu20-fs/': Invalid argument

Does anyone know what happened?

losetup:

NAME  SIZELIMIT  OFFSET  AUTOCLEAR  RO  BACK-FILE  DIO  LOG-SEC
...
/dev/block/loop4  0  0  0  0  /mnt/media_rw/6DE8-1BC6/loop.img  0  512

blkid | grep loop:

/dev/block/loop4: UUID="0746ba9e-fc09-4a32-8b5f-49aee9096cf7" BLOCK_SIZE="4096" TYPE="ext4"

unity – Why the loop is never working in the Waypoints?

using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.AI;

public class Waypoints : UnityEngine.MonoBehaviour
{
    public List<GameObject> waypoints = new List<GameObject>();
    public Animator _animator;
    public int waitTimeBeforeGo;
    public int num = 0;
    public float minDist;
    public float speed;
    public bool rand = false;
    public bool go = true;
    public bool loop = false;
    public bool waitTime = false;
    public float smoothRotation;

    private bool lastPoint = false;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        float dist = Vector3.Distance(gameObject.transform.position, waypoints(num).transform.position);

        if (waitTime == true)
        {
            StartCoroutine(WaitBeforeGo());
        }
        else
        {
            if (go)
            {
                //_animator.SetBool("Walk", true);

                if (dist > minDist && lastPoint == false)
                {
                    Move();
                }
                else
                {
                    if (!rand)
                    {
                        if (num + 1 == waypoints.Count)
                        {
                            if (loop == true)
                            {
                                num = 0;
                            }

                            lastPoint = true;
                            //_animator.SetBool("Idle", true);
                        }
                        else
                        {
                            num++;
                        }
                    }
                    else
                    {
                        num = Random.Range(0, waypoints.Count);
                    }
                }
            }
        }
    }

    public void Move()
    {
        Quaternion lookOnLook =
 Quaternion.LookRotation(waypoints(num).transform.position - transform.position);

        transform.rotation =
        Quaternion.Slerp(transform.rotation, lookOnLook, Time.deltaTime * smoothRotation);

        gameObject.transform.position += gameObject.transform.forward * speed * Time.deltaTime;
    }

    IEnumerator WaitBeforeGo()
    {
        yield return new WaitForSeconds(waitTimeBeforeGo);

        waitTime = false;
    }
}

For some reason when it’s getting to this part it keep doing this part over and over again like in a endless loop and never continue :

if (num + 1 == waypoints.Count)
                            {
                                if (loop == true)
                                {
                                    num = 0;
                                }
    
                                lastPoint = true;

It keep doing the lines num = 0 and lastPoint = true all the time in a loop and never continue to the rest of the code.

For example there is only 1 waypoint it’s moving to the waypoint but never get back it’s staying at the waypoint. The loop(ping pong) is never working.

postgresql – How to get a parallel nested loop in postgres 12?

I have a lateral join, and I would like each subselect to execute in parallel. According to the docs (https://www.postgresql.org/docs/12/parallel-plans.html), nested loops can be parallelized:

Just as in a non-parallel plan, the driving table may be joined to one or more other tables using a nested loop, hash join, or merge join. The inner side of the join may be any kind of non-parallel plan that is otherwise supported by the planner provided that it is safe to run within a parallel worker. Depending on the join type, the inner side may also be a parallel plan.

In a nested loop join, the inner side is always non-parallel. Although it is executed in full, this is efficient if the inner side is an index scan, because the outer tuples and thus the loops that look up values in the index are divided over the cooperating processes.

However, I can’t get a nested loop – or any join – to execute in parallel. Here’s a (contrived) example:

=> create table foo as
  select round(random()*100) as pid, val
  from generate_series(1, 10000000) v(val);
=> set force_parallel_mode = on;
=> explain
  SELECT pid, sum
  FROM (
    select distinct pid
    from foo
  ) x
  JOIN LATERAL (
    select sum(val)
    from foo y
    where x.pid = y.pid
  ) y
    ON TRUE;
                                  QUERY PLAN                                   
-------------------------------------------------------------------------------
 Nested Loop  (cost=358361.91..18288838.71 rows=101 width=16)
   ->  HashAggregate  (cost=179057.19..179058.20 rows=101 width=8)
         Group Key: foo.pid
         ->  Seq Scan on foo  (cost=0.00..154056.75 rows=10000175 width=8)
   ->  Aggregate  (cost=179304.72..179304.73 rows=1 width=8)
         ->  Seq Scan on foo y  (cost=0.00..179057.19 rows=99012 width=4)
               Filter: (foo.pid = pid)
 JIT:
   Functions: 12
   Options: Inlining true, Optimization true, Expressions true, Deforming true
(10 rows)

I know that parallel plans are sometimes generated on this database, here is an example:

=> explain select pid, sum(val) from foo group by 1;
                                          QUERY PLAN                                           
-----------------------------------------------------------------------------------------------
 Finalize GroupAggregate  (cost=92560.09..92577.55 rows=101 width=16)
   Group Key: pid
   ->  Gather Merge  (cost=92560.09..92574.52 rows=404 width=16)
         Workers Planned: 4
         ->  Sort  (cost=91560.03..91560.28 rows=101 width=16)
               Sort Key: pid
               ->  Partial HashAggregate  (cost=91555.66..91556.67 rows=101 width=16)
                     Group Key: pid
                     ->  Parallel Seq Scan on foo  (cost=0.00..79055.44 rows=2500044 width=12)
(9 rows)

postgres server version: 12.4

linux kernel version: 5.4

design – empty while loop: bad practice?

Consider the following:

boolean maybeUpdateTime() {
    if (this.timeReference.isAfter(lastInterval.getBeggining()) {
        this.timeReference = lastInterval.getEnd();
        lastInterval = intervalIterator.next();
        return true;
    }
    return false;
}

void updateState() {

    // do some stuff before

    while (maybeUpdateTime());

    // do more stuff afeter

}

Which roughly translates to: while it needs to be updated – update it.

Considering that maybeUpdateTime is a function/method that can be called independently, would it still be considered a bad practice to write such an empty while?

Otherwise I would have to create two functions, isTimeAfterInterval and updateTime, and call both every time?

I used this time and interval based exemple just to illustrate, but the specifics don’t matter to my question.

matplotlib – Python – resizing a plot in a for loop

I have a for loop which calculates a value (g) from a number of datasets, and plots it in a scatter graph:

g_meanarray = ()

import matplotlib.patches as mpatches

for i in range(len(dataset)):
    data=np.loadtxt(dataset(i),dtype=float,delimiter='/')
    distance=data(:,2)
    number=data(:,0)
    time=data(:,1)
    garray = ()
    
    g_error = errorfunc_g(distance,time)
    error_array = np.array(g_error)
    g_res=gravity(number,time,distance)
    garray.append(g_res)
    print(error_array)
    print(garray, 'n')
    
    g_mean = np.mean(garray)
    g_meanarray.append(g_mean)
    n = i + 1

    
    plt.scatter((n,n,n,n,n),garray, c = 'green', marker='.',label = "dataset{0}".format(i)) # Plots the measured g values for each dataset
    plt.errorbar((n,n,n,n,n),g_res,yerr=error_array, linestyle="None")
    plt.title('(Figure 1) - measured gravity values by dataset')
    plt.xlabel('Dataset')
    plt.ylabel('gravity estimated (m/s^2)')
    
    plt.scatter(n,g_mean,linestyle="None", label = "dataset{0}".format(i), c = 'black') # plots the mean g value for each dataset
    plt.title('(Figure 1) - measured gravity values by dataset')
    plt.xlabel('Dataset')
    plt.ylabel('gravity estimated (m/s^2)')
    meanlegend = mpatches.Patch(color='black', label='Mean g value')
    datalegend = mpatches.Patch(color='green', label='Measured g values')
    plt.legend(handles=(datalegend, meanlegend))

I want to resize the resulting graph, but using plt.figure(figsize = (a,b)) results in six different graphs for each dataset, I want them to remain plotted on one graph. How do I do this?

Thanks

Replying to a loop generated comment

I am trying to show my blog comments through foreach in the Comments.php file.
So as I saw with var_dumb(); in my

foreach($comments as $comment){
// my comment
}

, $comment does not have a reply link and I do not know how to display the replay link.
Does anybody seemed with this kind of issue I am not a professional and it is strange to me!

python – I am trying to get pandas to populate a cell using data from another cell in the same row and a for loop

import pandas as pd
import csv
from datetime import datetime
import random

pf = pd.read_csv(path, delimiter=',')  # for tests with original file
df = pd.read_csv('new_log.csv', delimiter=',')

for index, row in df.iterrows(): # iterate through rows in dataframe
    i = random.randint(0, 9) # select a random number for each row( This doesn't seem to do what I thought it would)
    if i > 6:  #  if the random number is high enough perform the operation
        row("IMC") = row("Block") * random.triangular(0, 1, 0)  # I want a column that already exists "IMC" to be populated by the value in the column "Block" multiplied by a random number
    else:   # if the random number is less than specified
        row("IMC") = 0
print(df)

df(“IMC”) = row(“Block”) * random.triangular(0, 1, 0) gives me almost what I want except it always provides a value greater than 0. I could round, but a bool would get me closer to what I want, which is why I am trying the for loop.

Result

IMC

NaN

NaN

NaN

My first problem is that the random number seems to only be chosen once instead of being chosen for each row, and I don’t really understand why.

My second problem is IMC has the value NaN. I tried writing it as df(row(IMC)) which gives me a separate NaN column with values in it.

I really want to know why the >i< is only being chosen once, instead of for each row. I would also like to know why I am not able to write any values into the dataframe as I have written it above.

I don’t know enough terminology to efficiently use the manual without ending up down a lot of irrelevant rabbit holes, so sorry for the basic level of the question.

unity – How can I change the script so it will not be with a while loop inside Update or using coroutine instead?

Try changing your Move() method into a Coroutine…

private IEnumerator Move() {
    //Your code until
    //
    while (stillTraveling) {
        //All your per-step code up until the end of the loop
        ...
        transform.position = newPos;
        yield return new WaitForFixedUpdate(); // Wait for the physics tick
    }
}

Start it running by calling StartCoroutine(Move());

Every time there’s a yield return, control will return to Unity. What you return determines how long Unity will wait before picking up again at the next line.

“Fixed Update” is the clock tick used by the physics engine, hence new WaitForFixedUpdate(). Others exist, eg you can return null to wait for the next frame.

As mentioned by DMGregory, you should only invoke this once.

You can do that using a simple boolean to track if it’s running. More usefully, StartCoroutine returns an objec of type Coroutine, so you can hold on to a reference..

Coroutine mover;
mover = StartCoroutine(...);

You can then use mover to cancel a running routine…

StopCoroutine(mover);

See https://docs.unity3d.com/ScriptReference/Coroutine.html for more information