optimization – SSA Copy Folding

When can we fold copies in SSA?

We are clearly allowed to fold a copy such as

a0 = call foo()
b0 = a0

But what about a program such as

a0 = call foo()
C0 = call bar() // ignore sinking briefly, assume this is used later
if(a0) {
    a1 = C0
}
a2 = phi(a0, a1)
call print(a2)

We can’t fold the a1 = C0 copy because phi(a0, C0) have altered meaning: both its parameters dominate the phi node and it will always evaluate to C0.

Two more cases:

a0 = call foo();
a1 = call bar();
do {
    a2 = phi(a0, t0)
    call print(a2)
    t0 = a1
} while(...)

Can’t fold the t0 = a1 copy as that would alter the phi’s meaning (it would become dominated by both parameters).

x0 = call foo()
C0 = call bar() // constant / loop invariant
do {
    a0 = C0
    if(x0) {
        a1 = call baz()
    }
    a2 = phi(a0, a1)
    call print(a2)
} while(...)

Can’t fold the a0 = C0 copy here because that makes the phi node (which becomes phi(C0, a1)) invalid across loop iterations. As soon as the x0 branch is taken, the phi(C0, a1) phi node can never evaluate to C0 in subsequent iterations.

Restrictions: I have a couple restrictions in my IR which may help the analysis:

  1. All loops are canonicalized into do-while loops so I can assume they run at least once
  2. The CFG is reducible and a phi node will always have two parameters

What I’ve tried:
Here’s the logic I’ve been able to come up with currently. Cannot fold a copy if

  • The copy (let’s call it a = b) has a phi node dependant and (a does not dominate the phi but b does)
  • Or the copy is in the same loop as one of its phi node dependants and the replacement (b) dominates the phi.

Question:
My question boils down to

  • How can I improve this logic
  • How can I better encapsulate the second case / generalize
  • Or, am I attempting to solve the wrong problem here?

unity – Folding selected Parent Game Object in the hierarchy

I am folding selected parent gameObject with this editor script. It all works well but it also folds other expanded gameObjects which should not happen. For example under two different parents, if I collapse one parent gameObject, even the other parent gameObject gets collapsed. How do I not let this happen?

The problem is at rootGameObjects.AddRange (SceneManager.GetSceneAt (i).GetRootGameObjects ()); where it takes the root gameObject in a scene but not the selected gameObject in the hierarchy.

using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;

public static class CollapseTools {
    private static int wait = 0;
    private static int undoIndex;

    public static void CollapseGameObjects () {
        EditorApplication.update -= CollapseGameObjects;
        CollapseGameObjects (new MenuCommand (null));
    }

    (MenuItem ("GameObject/Collapse GameObjects", priority = 40))
    private static void CollapseGameObjects (MenuCommand command) {
        // This happens when this button is clicked via hierarchy's right click context menu
        // and is called once for each object in the selection. We don't want that, we want
        // the function to be called only once
        if (command.context) {
            EditorApplication.update -= CollapseGameObjects;
            EditorApplication.update += CollapseGameObjects;

            return;
        }

        List<GameObject> rootGameObjects = new List<GameObject> ();
#if UNITY_2018_3_OR_NEWER
        // Check if a prefab stage is currently open
        var prefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage ();
        if (prefabStage != null && prefabStage.stageHandle.IsValid ())
            rootGameObjects.Add (prefabStage.prefabContentsRoot);
        else
#endif
        {
            int sceneCount = SceneManager.sceneCount;
            for (int i = 0; i < sceneCount; i++)
                rootGameObjects.AddRange (SceneManager.GetSceneAt (i).GetRootGameObjects ());
        }

        if (rootGameObjects.Count > 0) {
            Undo.IncrementCurrentGroup ();
            Selection.objects = Selection.objects;
            undoIndex = Undo.GetCurrentGroup ();

            Selection.objects = rootGameObjects.ToArray ();

            EditorApplication.update -= CollapseHelper;
            EditorApplication.update += CollapseHelper;
        }
    }

    private static void CollapseHelper () {
        if (wait < 1) // Increase the number if script doesn't always work
            wait++;
        else {
            EditorApplication.update -= CollapseHelper;
            wait = 0;

            EditorWindow focusedWindow = EditorWindow.focusedWindow;
            if (focusedWindow != null)
                focusedWindow.SendEvent (new Event { keyCode = KeyCode.LeftArrow, type = EventType.KeyDown, alt = true });

            Undo.RevertAllDownToGroup (undoIndex);
        }
    }
}
```

c – K&R 1-22 Folding Lines Exercise

I am currently reading and doing exercises from K&R. For exercise 1-22, I have the following solution. When I check my answer with other solutions online, I find that mine is generally shorter. Am I missing something? Any corrections are appreciated!

  1. Very long solution
  2. Very long solution 2
  3. Shorter solutions (some of the solutions at the top are around the same length as mine, but not generalized).

Exercise 1-22 Specifications: Write a program to “fold” long input lines into two or more shorter lines after the last non-blank character that occurs before the n-th column of input. Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column.

#include <stdio.h>

#define LEN 20
#define TABSTOP 8
#define MAXLINE 1000

int get_line(char s(), int lim);

int main() {
    int len, nc;
    char line(MAXLINE);
    while ((len = get_line(line, MAXLINE)) > 0) {
        nc = 0;
        for (int i = 0; i < len; i++) {
            if (nc >= LEN) {
                putchar('n');
                nc = 0;
                if (line(i) != 't') putchar(line(i));
            } else putchar(line(i));
            if (line(i) == 't') nc += (nc + 1) % TABSTOP;
            else ++nc;
        } 
    }
}

int get_line(char s(), int lim) {
    int c, i;

    for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != 'n'; ++i)
        s(i) = c;
    if (c == 'n') s(i++) = c;
    s(i) = '';
    return i;
}

email – disable case folding for pcre alias file in Postfix

As I described in my last question I’m suffering form an issue caused Postfix to doesn’t recognize local user account with capital letter!
I red some manual page of Postfix and ran into a quote saying pcre tables can be case sensitive! as you can see in this doc. there is an i option says by-default this option is on and tables are case-insensitive.


what I did was:

  1. created a /etc/postfix/pcrevirtual.pcre file with the below
    content:

/^shahab@mydomainname.com$/m Shahab

  1. change my virtual_alias_maps to pcre:/etc/postfix/pcrevirtual.pcre
  2. ran command sudo postmap -q 'shahab@mydomainname.com' pcre:/etc/postfix/pcrevirtual.pcre and it returned Shahab which is the correct name of my local user!!

Now, problem is still; and postfix still bounces my email because of (unknown user: "shahab")

Am I did wrong and pcre doesn’t seems to applied on alias?

Attention: please read my last question linked above!

dnd 5e – What happens if I sail a folding boat into an antimagic field?

Folding Boat is classified as a magical item despite the fact that its description doesn’t indicate that its different configurations are switched or maintained by magic:

This item also has three command words, each requiring you to use an action to speak it.

One command word causes the box to unfold into a boat (…).

The second command word causes the box to unfold into a ship (…).

When the box becomes a vessel, its weight becomes that of a normal vessel its size, and anything that was stored in the box remains in the boat.

The third command word causes the folding boat to fold back into a box (…).

So once the boat has been transformed into a box or a boat or a ship, no magical effect is required to maintain that shape/arrangement. The magic only applies to the transition between its shapes. This is important because Antimagic field says:

Within the Sphere, Spells can’t be cast, summoned creatures disappear, and even Magic Items become mundane.

Therefore, because the boat does not make use of a spell or summoned creature in anyway, it is subject to the third part of Antimagic Field’s effect: it becomes a mundane version of its normal magical self. To wit: a box or boat or ship, depending on whichever state it happens to be in.

The only difference between the Folding Boat being within an Antimagic Field and outside of one is that the command words that would normally cause it to change would have no effect while it was within the field. It would still function as a mundane boat or ship (or box) while within the field.

complexity theory – Is protein folding NP-hard and how to prove that?

This question has two facets that are related.

Is the general problem of protein folding really NP-hard?

The hydrophobic-polar protein folding model (Ken Dill et al., 1985) stated the problem on a lattice (similar to self-avoiding walk):

Simplification significantly reduces the computational effort in handling the model, although even in this simplified scenario the protein folding problem is NP-complete.

However, this simplification takes a problem which has some local convexity characteristics, protein folding can thought as finding the global minimum on some energy “cost function” as was depicted by Ken Dill et al., 2012:

enter image description here

As stated by others, translating a problem defined over continuous domain into problems over the integers or a binary lattice may introduce higher complexity cost. You may think about it as taking a linear problem and solve it using 3SAT solver, any problem in $P$ is presumably reducible to 3SAT. I assume that there is a convex nature to the energy landscape, and this piecewise convexity is beneficial for the search for the minimal energy state.

How to reduce hydrophobic-polar protein folding to TSM/3SAT/CNF?

The work by Bahi et al. really confused me. I expected it to be something relatively simple to reduce 3SAT or TSM to the problem of protein lattice, because intuitively it seems that finding Hamiltonian path could be stated as a finding a protein conformation on a lattice.

Bahi, Jacques M., et al. Computational investigations of folded
self-avoiding walks related to protein folding. Computational biology
and chemistry 47 (2013): 246-256.

Bahi, Jacques M., et al. Chaos of protein folding. In IJCNN 2011,
Int. Joint Conf. on Neural Networks, pages 1948–1954, San Jose,
California, United States, July 2011.

Bahi, Jacques M., et al. Protein folding in the 2D
hydrophobic-hydrophilic (HP) square lattice model is chaotic.
Cognitive Computation, 4(1):98–114, 2012.

Bahi, Jacques M., et al. Is protein folding problem really a
NP-complete one ? First investigations. arXiv:1306.1372, October 2012.
Submitted to Journal of Bioinformatics and Computational Biology
(Elsevier).

Bahi, Jacques M., et al. Unfoldable self-avoiding walks are infinite.
Consequences for the protein structure prediction problem. arXiv.org,
2013.

Bahi, Jacques M., et al. Protein structure prediction software
generate two different sets of conformations. Or the study of unfolded
self-avoiding walks. arXiv:1306.1439, 2013.

complexity theory – Is protein folding NP-hard?

This question has two facets that are related.

Is the general problem of protein folding really NP-hard?

The hydrophobic-polar protein folding model (Ken Dill et al., 1985) stated the problem on a lattice (similar to self-avoiding walk):

Simplification significantly reduces the computational effort in handling the model, although even in this simplified scenario the protein folding problem is NP-complete.

However, this simplification takes a problem which has some local convexity characteristics, protein folding can thought as finding the global minimum on some energy “cost function” as was depicted by Ken Dill et al., 2012:

enter image description here

As stated by others, translating a problem defined over continuous domain into problems over the integers or a binary lattice may introduce higher complexity cost. You may think about it as taking a linear problem and solve it using 3SAT solver, any problem in $P$ is presumably reducible to 3SAT. I assume that there is a convex nature to the energy landscape, and this piecewise convexity is beneficial for the search for the minimal energy state.

How to reduce hydrophobic-polar protein folding to TSM/3SAT/CNF?

The work by Bahi et al. really confused me. I expected it to be something relatively simple to reduce 3SAT or TSM to the problem of protein lattice, because intuitively it seems that finding Hamiltonian path could be stated as a finding a protein conformation on a lattice.

Bahi, Jacques M., et al. Computational investigations of folded
self-avoiding walks related to protein folding. Computational biology
and chemistry 47 (2013): 246-256.

Bahi, Jacques M., et al. Chaos of protein folding. In IJCNN 2011,
Int. Joint Conf. on Neural Networks, pages 1948–1954, San Jose,
California, United States, July 2011.

Bahi, Jacques M., et al. Protein folding in the 2D
hydrophobic-hydrophilic (HP) square lattice model is chaotic.
Cognitive Computation, 4(1):98–114, 2012.

Bahi, Jacques M., et al. Is protein folding problem really a
NP-complete one ? First investigations. arXiv:1306.1372, October 2012.
Submitted to Journal of Bioinformatics and Computational Biology
(Elsevier).

Bahi, Jacques M., et al. Unfoldable self-avoiding walks are infinite.
Consequences for the protein structure prediction problem. arXiv.org,
2013.

Bahi, Jacques M., et al. Protein structure prediction software
generate two different sets of conformations. Or the study of unfolded
self-avoiding walks. arXiv:1306.1439, 2013.

mg.metric geometry – Folding a non-rectangular shape into a rectangle of uniform thickness

I think the following might be an interesting subproblem of this question:

Question: For an odd number $nge 3$, is there a non-rectangular but still convex shape for a piece of paper of area $A=1$, that can be folded (no tearing) into a rectangle of area $1/n$ of uniform thickness, that is, the resulting rectangle has $n$ layers of paper everywhere?


It is easy for every $n$ when dropping convexity.

It is also easy for $n=2$ (see the parallelogram below), and once you have a rectangle, you can get any multiple of $n$ too. This is why I ask for odd numbers (one could ask for primes only).

I have yet no solution for $n=3$ (nor an argument why it should not be possible).
I have a solution if the result needs not be a rectangle.

But as far as I can tell, the resulting triangle cannot be made into a 9-layer folding.
This is why rectangles are nicer.

I am not restricting to polygonal shapes in my question. I am aware that e.g. a circular disc can be made into an $n$-layer folding for all $n$.
But it does probably not admit an $n$-layer folding into a rectangle.

mg.metric geometry – On folding a polygonal sheet

Consider a polygonal sheet $P$ with $N$ vertices (it material is not stretchable or tearable). Let $n$ be a positive integer >=2.

Question: Let $P$ lie on a flat plane. We need to fold up $P$ so that it now occupies only an area $A/n$ of the plane. It is also needed that the folding is as uniform as possible – ie the number of layers of the material above any given point should be as close to $n$ as possible. We need an algorithm that does it and an estimate of its complexity.

Example: If $P$ is a rectangle of area $A$ and $n$ is an integer, it is easy to see that we can fold it to an area $A/n$ such that it is exactly $n$ layers thick throughout – the ‘creases’ could simply be $n-1$ equally spaced parallel lines. It appears that no other shape of $P$ has this property of ‘perfectly uniform foldability’. Which is the shape(s) of $P$ that causes greatest variation in the number of layers for a given $n$?

Further possibilities: One can further ask: Minimize the perimeter of the area $A/n$ region that is covered by the folded polygon. Alternatively, We could require $P$ to be not just folded but to be folded into a rectangular or square shape – so that it can be packed into a rectangular or square box of base area $A/n$.

payment – what is the best way to combine a disposable card with a folding panel?

I am working on a payment flow where a cart summary panel appears on the right side of the payment form. We want the user to be able to remove a product, but due to legal and compliance reasons we should also be able to display more information related to the product.

In saying that, there is a lot of information that should be displayed on each product, so we want to hide it until the user reveals it. There may be 1 to 3 products in the cart at a time, so showing all the information instead of hiding it makes the summary too large and difficult to read / scan.

We are exploring something like the following as I have not come across any similar patterns during my travels. We also looked at swapping the Delete and Hide / Show buttons to make it more of an accordion / folding panel pattern, but it didn't seem right.

My question is does an approach like this make sense? Or is there another way to solve that I'm missing?

Summary of purchase