Diag Function to create a matrix - matrix

How would I go about creating the matrix
[1 2 0 0 0;
-1 1 2 0 0;
0 -1 1 2 0;
0 0 -1 1 2;
0 0 0 -1 1]
using the diag command in MatLab?

Here is one way:
> diag(ones(1,5),0)+diag(ones(1,4),1)*2+diag(ones(1,4),-1)*-1
ans =
1 2 0 0 0
-1 1 2 0 0
0 -1 1 2 0
0 0 -1 1 2
0 0 0 -1 1
>
This just creates three diagonals at 0, +1 and -1, scales them as needed, then adds them.

Related

Parse error when building my matrix, do not understand why

I'm trying to build a matrix out of linear equations but for some reason I keep getting a parse error in my matrix when previously I did not.
CoM=[K1*(abs(Q1).^r) K2*(abs(Q2).^r) -(K3*(abs(Q3).^r)) -(K4*(abs(Q4).^r)); K3*(abs(Q3).^r) -(K5*(abs(Q5).^r)) -(K6*(abs(Q6).^r) -(K7*(abs(Q7).^r) ; 1 -1 0 0 ; 1 1 -1 0 ; 1 -1 0 0 ; 1 -1 0 0 ; 1 1 -1 0];
^
error: parse error near line 1 of file '____'\WNA2loop.m
syntax error
CoM=[K1*(abs(Q1).^r) K2*(abs(Q2).^r) -(K3*(abs(Q3).^r)) -(K4*(abs(Q4).^r)); K3*(abs(Q3).^r) -(K5*(abs(Q5).^r)) -(K6*(abs(Q6).^r) -(K7*(abs(Q7).^r) ; 1 -1 0 0 ; 1 1 -1 0 ; 1 -1 0 0 ; 1 -1 0 0 ; 1 1 -1 0];
^
Right where I put the caret but when I take that small part of the matrix and run the command it
[1 -1 0 0 ; 1 1 -1 0 ; 1 -1 0 0 ; 1 -1 0 0 ; 1 1 -1 0]
ans = 1 -1 0 0
1 1 -1 0
1 -1 0 0
1 -1 0 0
1 1 -1 0
The K1 to K7 and Q1 to Q7 as well as the are just variables that get input by the user through the input function it worked before but now it just wont budge, could someone please provide assistance on this?

Is there a way to show the index of atoms in rdkit.Chem.rdmolops.GetAdjacencyMatrix?

I'm trying to convert a compound from mol to adjacency matrix. However, i encountered a problem that rdkit.Chem.rdmolops.GetAdjacencyMatrix() doesn't provide the index of the atoms for the adjacency matrix. Is there any way to include the index data for the adjacency matrix in rdkit?
rdkit.Chem.rdmolops.GetAdjacencyMatrix((Mol)mol)
As the RDKit AdjacencyMatrix is ordered from zero upwards, you can convert it to a Pandas dataframe.
from rdkit import Chem
import pandas as pd
s = 'CCC(C(O)C)CN'
mol = Chem.MolFromSmiles(s)
am = Chem.GetAdjacencyMatrix(mol)
print(am)
[[0 1 0 0 0 0 0 0]
[1 0 1 0 0 0 0 0]
[0 1 0 1 0 0 1 0]
[0 0 1 0 1 1 0 0]
[0 0 0 1 0 0 0 0]
[0 0 0 1 0 0 0 0]
[0 0 1 0 0 0 0 1]
[0 0 0 0 0 0 1 0]]
df = pd.DataFrame(am)
print(df)
0 1 2 3 4 5 6 7
0 0 1 0 0 0 0 0 0
1 1 0 1 0 0 0 0 0
2 0 1 0 1 0 0 1 0
3 0 0 1 0 1 1 0 0
4 0 0 0 1 0 0 0 0
5 0 0 0 1 0 0 0 0
6 0 0 1 0 0 0 0 1
7 0 0 0 0 0 0 1 0
If you want elements instead of indices
element = [atom.GetSymbol() for atom in mol.GetAtoms()]
print(element)
['C', 'C', 'C', 'C', 'O', 'C', 'C', 'N']
df_e = pd.DataFrame(am, index=element, columns=element)
print(df_e)
C C C C O C C N
C 0 1 0 0 0 0 0 0
C 1 0 1 0 0 0 0 0
C 0 1 0 1 0 0 1 0
C 0 0 1 0 1 1 0 0
O 0 0 0 1 0 0 0 0
C 0 0 0 1 0 0 0 0
C 0 0 1 0 0 0 0 1
N 0 0 0 0 0 0 1 0

Algorithm to divide black and white chocolate bar with least number of breaks

I have a rectangular chocolate bar that consists of squares either black, white or mixed. The bar being not bigger than 50x50 squares. I'm supposed to divide the bar between two people(one gets all the white squares and one the black ones, mixed ones don't matter), by cracking it either horizontally or vertically. I'm supposed to find a method with the least amount of such cracks.
I'm given this input:
M N (number of rows, number of columns)
and then M rows that are N numbers long(0 means white, 1 means black, 2 is mixed)
so for example bar described like this:
4 4
0 1 1 1
1 0 1 0
1 0 1 0
2 0 0 0
can be divided by cracking it seven times in total.
And this one needs at least 24 cracks:
5 8
0 1 0 1 0 1 0 2
1 0 2 0 2 0 1 0
0 2 0 2 0 1 0 2
1 0 2 0 2 0 1 0
0 1 0 1 0 1 0 2
I was thinking about something like cracking the bar in two pieces so that the sum of sums of future cracks needed to divide two newly made chocolate bar pieces is the least possible from all the possible cracks (which is height -1 + width -1 of the current bar piece being cracked)
I managed, also thanks to zenwraight, to write a code that solves this but I encountered another problem, it is really inefficient and if the starting chocolate bar gets bigger than 30x30 it's practically unusable.
Anyway the source code (written in C):
#include <stdio.h>
#include <stdlib.h>
const int M, N;
int ****pieces;
int r = 0;
int ri = 0;
int inf;
void printmatrix(int **mat, int starti, int startj, int maxi, int maxj) {
for (int i = starti; i < maxi; i++) {
for (int j = startj; j < maxj; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
int minbreaks(int **mat, int starti, int startj, int maxi, int maxj, int depth) {
if (pieces[starti][startj][maxi][maxj] != 0) {
r++;
return pieces[starti][startj][maxi][maxj];
} else {
ri++;
int vbreaks[maxj - 1];
int hbreaks[maxi - 1];
for (int i = 0; i < maxj; i++) {
vbreaks[i] = inf;
}
for (int i = 0; i < maxi; i++) {
hbreaks[i] = inf;
}
int currentmin = inf;
for (int i = starti; i < maxi; i++) {
for (int j = startj; j < maxj - 1; j++) {//traverse trough whole matrix
if (mat[i][j] != 2) {
for (int k = startj + 1; k < maxj; k++) {//traverse all columns
if (vbreaks[k - 1] == inf) {//traverse whole column
for (int z = starti; z < maxi; z++) {
if (mat[z][k] != 2 && mat[i][j] != mat[z][k]) {
/* printmatrix(mat, starti, startj, maxi, maxj);
printf("brokenv in depth:%d->\n", depth);
printmatrix(mat, starti, startj, maxi, k);
printf("and\n");
printmatrix(mat, starti, k, maxi, maxj);
printf("****\n");*/
vbreaks[k - 1] = minbreaks(mat, starti, startj, maxi, k, depth + 1) + minbreaks(mat, starti, k, maxi, maxj, depth + 1);
if (vbreaks[k - 1] < currentmin) {
currentmin = vbreaks[k - 1];
}
break;
}
}
}
}
}
}
}
for (int i = starti; i < maxi - 1; i++) {
for (int j = startj; j < maxj; j++) {
if (mat[i][j] != 2) {
for (int k = starti + 1; k < maxi; k++) {
if (hbreaks[k - 1] == inf) {
for (int z = startj; z < maxj; z++) {
if (mat[k][z] != 2 && mat[i][j] != mat[k][z]) {
/* printmatrix(mat, starti, startj, maxi, maxj);
printf("brokenh in depth:%d->\n", depth);
printmatrix(mat, starti, startj, k, maxj);
printf("and\n");
printmatrix(mat, k, startj, maxi, maxj);
printf("****\n");*/
hbreaks[k - 1] = minbreaks(mat, starti, startj, k, maxj, depth + 1) + minbreaks(mat, k, startj, maxi, maxj, depth + 1);
if (hbreaks[k - 1] < currentmin) {
currentmin = hbreaks[k - 1];
}
break;
}
}
}
}
}
}
}
if (currentmin == inf) {
currentmin = 1;
}
pieces[starti][startj][maxi][maxj] = currentmin;
return currentmin;
}
}
void alloc(int i, int j) {
pieces[i][j] = malloc(sizeof (int*)*(M + 1));
for (int y = i; y < M + 1; y++) {
pieces[i][j][y] = malloc(sizeof (int)*(N + 1));
for (int x = j; x < N + 1; x++) {
pieces[i][j][y][x] = 0;
}
}
}
int main(void) {
FILE *file = fopen("pub08.in", "r");
//FILE *file = stdin;
fscanf(file, "%d %d", &M, &N);
int **mat = malloc(sizeof (int*)*M);
pieces = malloc(sizeof (int***)*M);
for (int i = 0; i < M; i++) {
mat[i] = malloc(sizeof (int)*N);
pieces[i] = malloc(sizeof (int**)*N);
for (int j = 0; j < N; j++) {
int x;
fscanf(file, "%d", &x);
mat[i][j] = x;
alloc(i, j);
}
}
inf = M * (M + 1) * N * (N + 1) / 4 + 1;
int result = minbreaks(mat, 0, 0, M, N, 0);
printf("%d\n", result);
printf("ri:%d,r:%d\n", ri, r);
return (EXIT_SUCCESS);
}
I am aiming to solve this input :
40 40
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
in under 2 seconds, which is much much faster time than that of my current program. The minimum amount of cracks for this one is 126.
Nice question, I have an approach in mind which makes use of recursion to tackle the above problem.
So as at each level or step you have two options either to split the bar horizontally or vertically.
So let's understand the algorithm with an example.
Example:-
4 4
0 1 1 1
1 0 1 0
1 0 1 0
2 0 0 0
Now let's call our function minBreaks(int n, int m, int matleft, int right)
So at first step if we break is horizontally our matleft will be
0
1
1
2
and matright will be
1 1 1
0 1 0
0 1 0
0 0 0
Now similarly if we had broken this vertically our matleft will be
0 1 1 1
and matright will be
1 0 1 0
1 0 1 0
2 0 0 0
Now you pass along this matleft and matright in next recursion call
And then at each call when size of row = 1 or col = 1, you can check the connected components of same value and return the count of connected components
Like for example for vertically case of maxleft -> 0 1 1 1, you will return 2.
Similarly for all the cases and the end part of the method will return
min between break horizontally and vertically
Hope this helps!

Use an awk loop to subset a file

I have a file with lots of pieces of information that I want to split on the first column.
Example (example.gen):
1 rs3094315 752566 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
1 rs2094315 752999 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
2 rs3044315 759996 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
2 rs3054375 799966 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
2 rs3094375 999566 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
3 rs3078315 799866 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
3 rs4054315 759986 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
4 rs4900215 752998 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
4 rs5094315 759886 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
4 rs6094315 798866 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
Desired output:
Chr1.gen
1 rs3094315 752566 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
1 rs2094315 752999 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
Chr2.gen
2 rs3044315 759996 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
2 rs3054375 799966 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
2 rs3094375 999566 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
Chr3.gen
3 rs3078315 799866 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
3 rs4054315 759986 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
Chr4.gen
4 rs4900215 752998 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
4 rs5094315 759886 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
4 rs6094315 798866 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
I've tried to do this with the following shell scripts, but it doesn't work - I can't work out how to get awk to recognise a variable defined outside the awk script itself.
First script attempt (no awk loop):
for i in {1..23}
do
awk '{$1 = $i}' example.gen > Chr$i.gen
done
Second script attempt (with awk loop):
for i in {1..23}
do
awk '{for (i = 1; i <= 23; i++) $1 = $i}' example.gen > Chr$i.gen
done
I'm sure its probably quite basic, but I just can't work it out...
Thank you!
With awk:
awk '{print > "Chr"$1".gen"}' file
It just prints and redirects it to a file. And how is this file defined? With "Chr" + first_column + ".gen".
With your sample input it creates 4 files. For example the 4th is:
$ cat Chr4.gen
4 rs4900215 752998 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
4 rs5094315 759886 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
4 rs6094315 798866 A G 0 1 0 1 0 0 1 0 0 0 1 0 0 1
First, use #fedorqui's answer, as that is best. But to understand the mistake you made with your first attempt (which was close), read on.
Your first attempt failed because you put the test inside the action (in the braces), not preceding it. The minimal fix:
awk "\$1 == $i" example.gen > Chr$i.gen
This uses double quotes to allow the value of i to be seen by the awk script, but that requires you to then escape the dollar sign for $1 so that you don't substitute the value of the shell's first positional argument. Cleaner but longer:
awk -v i=$i '$1 == i' example.gen > Chr$i.gen
This adds creates a variable i inside the awk script with the same value as the shell's i variable.

C/C++ wavelet library that return also the NxN wavelet matrix

I am looking for a C++ library for Discrete Wavelet Transform (DWT) which can also return
the NxN DWT matrix of the transform.
There was a similar question opened here
Looking for a good C/C++ wavelet library for signal processing
but I am looking for something more specific as you can see.
It would be more helpful if the library is under some non-GNU license that lets me use it in proprietary software (LGPL, MPL, BSD etc.)
Thanks in advance
The reason why this matrix is never computed is that it is very inefficient to compute the DWT using it. The FWT approach is much faster.
For a signal of length 16 and a 3-level haar transform, I found that this matrix in matlab
>> h=[1 1];
>> g=[1 -1];
>> m1=[[ones(1,8) zeros(1,8); ...
zeros(1,8) ones(1,8); ...
1 1 1 1 -1 -1 -1 -1 zeros(1,8); ...
zeros(1,8) 1 1 1 1 -1 -1 -1 -1]/sqrt(8); ...
[1 1 -1 -1 zeros(1,12); ...
zeros(1,4) 1 1 -1 -1 zeros(1,8); ...
zeros(1,8) 1 1 -1 -1 zeros(1,4); ...
zeros(1,12) 1 1 -1 -1]/sqrt(4); ...
[g zeros(1,14); ...
zeros(1,2) g zeros(1,12); ...
zeros(1,4) g zeros(1,10); ...
zeros(1,6) g zeros(1,8); ...
zeros(1,8) g zeros(1,6); ...
zeros(1,10) g zeros(1,4); ...
zeros(1,12) g zeros(1,2); ...
zeros(1,14) g]/sqrt(2)]
m1 =
A A A A A A A A 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 A A A A A A A A
A A A A -A -A -A -A 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 A A A A -A -A -A -A
B B -B -B 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 B B -B -B 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 B B -B -B 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 B B -B -B
C -C 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 C -C 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 C -C 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 C -C 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 C -C 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 C -C 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 C -C 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 C -C
where A=1/sqrt(8), B=1/sqrt(4) and C=1/sqrt(2).
corresponds to the FWT. That shows you how you build your matrix from the filters. You start with the bottom half of the matrix --a matrix of zeroes, putting filter g 2 steps further every row. then make the filter twice as wide and repeat, only now shift 4 steps at a time. repeat this until you are at the highest level of decomposition, the finally put the approximation filter in at the same width (here, 8).
just as a check
>> signal=1:16; % ramp
>> [h g]=daubcqf(2); % Haar coefficients from the Rice wavelet toolbox
>> fwt(h,signal,3) % fwt code by Jeffrey Kantor
>> m1*signal' % should produce the same vector
Hope that helps you writing it in C++. It is not difficult (a bit of bookkeeping) but as said, noone uses it because efficient algorithms do not need it.

Resources