How to export global load vector in abaqus - abaqus

In FEM, we needs to solve K*u=P, where K is global stiffness matrix, u is displacement, P is global load vector.
I want export global load vector, that is P.
I have a read at the manual, I add follow lines into the inp file.
*STEP
*MATRIX GENERATE, STIFFNESS, LOAD
*MATRIX OUTPUT, STIFFNESS, LOAD, FORMAT=MATRIX INPUT
*END STEP
After add the lines, then run the inp file, I can export the global stiffness matrix, I can see a "Job-1_STIF2.mtx" in the work directory, but there is nothing relate to global load vector. I do not know why the load vector can not be export.
Can anyone help me? Could you please modify my inp file? Or give any suggestions? Or give me an example inp that can export global load vector? Thanks for your time.
The full inp file shown as follow
*Heading
** Job name: Job-1 Model name: Job_my
** Generated by: Abaqus/CAE 2016
*Preprint, echo=NO, model=NO, history=NO, contact=NO
**
** PARTS
**
*Part, name=PART-1
*Node
1, 0., 0., 0.
2, 0., 10., 0.
3, 10., 0., 0.
4, 10., 10., 0.
5, 0., 0., 5.
6, 0., 10., 5.
7, 10., 0., 5.
8, 10., 10., 5.
9, 0., 0., 10.
10, 0., 10., 10.
11, 10., 0., 10.
12, 10., 10., 10.
*Element, type=C3D4
1, 1, 3, 4, 8
2, 1, 3, 8, 5
3, 3, 8, 5, 7
4, 6, 5, 1, 8
5, 6, 1, 2, 4
6, 6, 1, 4, 8
7, 5, 7, 8, 12
8, 5, 7, 12, 9
9, 7, 12, 9, 11
10, 10, 9, 5, 12
11, 10, 5, 6, 8
12, 10, 5, 8, 12
*Elset, elset=Set-1, generate
1, 12, 1
** Section: Section-1
*Solid Section, elset=Set-1, material=MATERIAL-1
,
*End Part
**
**
** ASSEMBLY
**
*Assembly, name=Assembly
**
*Instance, name=PART-1-1, part=PART-1
*End Instance
**
*Nset, nset=Set-1, instance=PART-1-1, generate
1, 4, 1
*Nset, nset=Set-2, instance=PART-1-1
1, 2, 5, 6, 9, 10
*Nset, nset=Set-3, instance=PART-1-1, generate
1, 11, 2
*Elset, elset=_Surf-1_S2, internal, instance=PART-1-1
10,
*Elset, elset=_Surf-1_S3, internal, instance=PART-1-1
9,
*Surface, type=ELEMENT, name=Surf-1
_Surf-1_S2, S2
_Surf-1_S3, S3
*End Assembly
**
** MATERIALS
**
*Material, name=MATERIAL-1
*Elastic
100.,0.3
**
** BOUNDARY CONDITIONS
**
** Name: BC-1 Type: Displacement/Rotation
*Boundary
Set-1, 3, 3
** Name: BC-2 Type: Displacement/Rotation
*Boundary
Set-2, 1, 1
** Name: BC-3 Type: Displacement/Rotation
*Boundary
Set-3, 2, 2
** ----------------------------------------------------------------
**
** STEP: Step-1
**
*Step, name=Step-1, nlgeom=NO
*Static
1., 1., 1e-05, 1.
*Element Matrix Output,ELSET=PART-1-1.Set-1,
DLOAD=YES,File Name=element_matrix_vector,Frequency=1,Output File=User Defined,Stiffness=Yes,Mass=Yes
**
** LOADS
**
** Name: Load-1 Type: Pressure
*Dsload
Surf-1, P, 1.
**
** OUTPUT REQUESTS
**
*Restart, write, frequency=0
**
** FIELD OUTPUT: F-Output-1
**
*Output, field, variable=PRESELECT
**
** HISTORY OUTPUT: H-Output-1
**
*Output, history, variable=PRESELECT
*End Step
*STEP
*MATRIX GENERATE, STIFFNESS, LOAD
*MATRIX OUTPUT, STIFFNESS, LOAD, FORMAT=MATRIX INPUT
*END STEP

The load should also be defined in the step which export the matrix.
That is, the line add to generate global system matrix should be:
*STEP
*MATRIX GENERATE, STIFFNESS, LOAD
*MATRIX OUTPUT, STIFFNESS, LOAD, FORMAT=MATRIX INPUT
**
** LOADS
**
** Name: Load-1 Type: Pressure
*Dsload
Surf-1, P, 1.
*END STEP
full inp file shown as follows:
*Heading
** Job name: Job-1 Model name: Job_my
** Generated by: Abaqus/CAE 2016
*Preprint, echo=NO, model=NO, history=NO, contact=NO
**
** PARTS
**
*Part, name=PART-1
*Node
1, 0., 0., 0.
2, 0., 10., 0.
3, 10., 0., 0.
4, 10., 10., 0.
5, 0., 0., 5.
6, 0., 10., 5.
7, 10., 0., 5.
8, 10., 10., 5.
9, 0., 0., 10.
10, 0., 10., 10.
11, 10., 0., 10.
12, 10., 10., 10.
*Element, type=C3D4
1, 1, 3, 4, 8
2, 1, 3, 8, 5
3, 3, 8, 5, 7
4, 6, 5, 1, 8
5, 6, 1, 2, 4
6, 6, 1, 4, 8
7, 5, 7, 8, 12
8, 5, 7, 12, 9
9, 7, 12, 9, 11
10, 10, 9, 5, 12
11, 10, 5, 6, 8
12, 10, 5, 8, 12
*Elset, elset=Set-1, generate
1, 12, 1
** Section: Section-1
*Solid Section, elset=Set-1, material=MATERIAL-1
,
*End Part
**
**
** ASSEMBLY
**
*Assembly, name=Assembly
**
*Instance, name=PART-1-1, part=PART-1
*End Instance
**
*Nset, nset=Set-1, instance=PART-1-1, generate
1, 4, 1
*Nset, nset=Set-2, instance=PART-1-1
1, 2, 5, 6, 9, 10
*Nset, nset=Set-3, instance=PART-1-1, generate
1, 11, 2
*Elset, elset=_Surf-1_S2, internal, instance=PART-1-1
10,
*Elset, elset=_Surf-1_S3, internal, instance=PART-1-1
9,
*Surface, type=ELEMENT, name=Surf-1
_Surf-1_S2, S2
_Surf-1_S3, S3
*End Assembly
**
** MATERIALS
**
*Material, name=MATERIAL-1
*Elastic
100.,0.3
**
** BOUNDARY CONDITIONS
**
** Name: BC-1 Type: Displacement/Rotation
*Boundary
Set-1, 3, 3
** Name: BC-2 Type: Displacement/Rotation
*Boundary
Set-2, 1, 1
** Name: BC-3 Type: Displacement/Rotation
*Boundary
Set-3, 2, 2
** ----------------------------------------------------------------
**
** STEP: Step-1
**
*Step, name=Step-1, nlgeom=NO
*Static
1., 1., 1e-05, 1.
*Element Matrix Output,ELSET=PART-1-1.Set-1,
DLOAD=YES,File Name=element_matrix_vector,Frequency=1,Output File=User Defined,Stiffness=Yes,Mass=Yes
**
** LOADS
**
** Name: Load-1 Type: Pressure
*Dsload
Surf-1, P, 1.
**
** OUTPUT REQUESTS
**
*Restart, write, frequency=0
**
** FIELD OUTPUT: F-Output-1
**
*Output, field, variable=PRESELECT
**
** HISTORY OUTPUT: H-Output-1
**
*Output, history, variable=PRESELECT
*End Step
*STEP
*MATRIX GENERATE, STIFFNESS, LOAD
*MATRIX OUTPUT, STIFFNESS, LOAD, FORMAT=MATRIX INPUT
**
** LOADS
**
** Name: Load-1 Type: Pressure
*Dsload
Surf-1, P, 1.
*END STEP

Related

How can we reduce the size of the graph generated by Maximal Clique and remove the nodes of specific cliques?

I am using the networkx—find_cliques library for finding the maximal cliques in a graph. I want to reduce the size of this graph based on maximal cliques.
Here is the code:
from torch_geometric.utils.convert
import to_networkx from torch_geometric.data import Data
import networkx as nx
edge_list = torch.tensor([
[0, 1, 1, 2, 2, 2, 3, 4, 5, 6, 6, 6, 7, 7, 8 ], # Source Nodes
[1, 2, 3, 4, 5, 3, 9, 5, 6, 7, 8, 9, 8, 9, 9 ] # Target Nodes
], dtype=torch.long)
node_features = torch.tensor([
[-8, 1, 5, 8, 2, -3], # Features of Node 0
[-1, 0, 2, -3, 0, 1], # Features of Node 1
[1, -1, 0, -1, 2, 1], # Features of Node 2
[0, 1, 4, -2, 3, 4], # Features of Node 3
],dtype=torch.long)
data = Data(x=node_features, edge_index=edge_list, edge_attr=edge_weight)
G_directed = to_networkx(data)
G_undirected = G_directed.to_undirected()
no_cliques= nx.find_cliques(G, nodes=None)
print(No_cliques)
List of Maximal Cliques = {1,[1,2], 2,[2, 3, 4], 3,[4, 5, 6], 4,[6, 7], 5, [7, 8, 9, 10], 6, [10,3]}
In the next step, we reduce the size of the original graph in the coarsened graph as we consider one clique as one node and joint the edge based on this rule, joining two cliques if they are not disjoint. I want to remove such a clique whose node already appeared in other cliques. In the above example, the nodes of clique 6 are already assigned in cliques no 2 and 5. So in the new graph, this clique should be removed from the clique list.
For better understanding, I am posting the picture.
hierarchy of a graph as the coarsened graph at each level
I want to make this type of graph hierarchy based on maximal clique. Does anyone know about it? How can I do it?

How to generate conditions within constraints in Z3py

Let us assume there are 5-time slots and at each time slot, I have 4 options to choose from, each with a known reward, for eg. rewards = [5, 2, 1, -3]. At every time step, at least 1 of the four options must be selected, with a condition that, if option 3 (with reward -3) is chosen at a time t, then for the remaining time steps, none of the options should be selected. As an example, considering the options are indexed from 0, both [2, 1, 1, 0, 3] and [2, 1, 1, 3, 99] are valid solutions with the second solution having option 3 selected in the 3rd time step and 99 is some random value representing no option was chosen.
The Z3py code I tried is here:
T = 6 #Total time slots
s = Solver()
pick = [[Bool('t%d_ch%d' %(j, i)) for i in range(4)] for j in range(T)]
# Rewards of each option
Rewards = [5, 2, 1, -3]
# Select at most one of the 4 options as True
for i in range(T):
s.add(Or(Not(Or(pick[i][0], pick[i][1], pick[i][2], pick[i][3])),
And(Xor(pick[i][0],pick[i][1]), Not(Or(pick[i][2], pick[i][3]))),
And(Xor(pick[i][2],pick[i][3]), Not(Or(pick[i][0], pick[i][1])))))
# If option 3 is picked, then none of the 4 options should be selected for the future time slots
# else, exactly one should be selected.
for i in range(len(pick)-1):
for j in range(4):
s.add(If(And(j==3,pick[i][j]),
Not(Or(pick[i+1][0], pick[i+1][1], pick[i+1][2], pick[i+1][3])),
Or(And(Xor(pick[i+1][0],pick[i+1][1]), Not(Or(pick[i+1][2], pick[i+1][3]))),
And(Xor(pick[i+1][2],pick[i+1][3]), Not(Or(pick[i+1][0], pick[i+1][1]))))))
if s.check()==False:
print("unsat")
m=s.model()
print(m)
With this implementation, I am not getting solutions such as [2, 1, 1, 3, 99]. All of them either do not have option 3 or have it in the last time slot.
I know there is an error inside the If part but I'm unable to figure it out. Is there a better way to achieve such solutions?
It's hard to decipher what you're trying to do. From a basic reading of your description, I think this might be an instance of the XY problem. See https://xyproblem.info/ for details on that, and try to cast your question in terms of what your original goal is; instead of a particular solution, you're trying to implement. (It seems to me that the solution you came up with is unnecessarily complicated.)
Having said that, you can solve your problem as stated if you get rid of the 99 requirement and simply indicate -3 as the terminator. Once you pick -3, then all the following picks should be -3. This can be coded as follows:
from z3 import *
T = 6
s = Solver()
Rewards = [5, 2, 1, -3]
picks = [Int('pick_%d' % i) for i in range(T)]
def pickReward(p):
return Or([p == r for r in Rewards])
for i in range(T):
if i == 0:
s.add(pickReward(picks[i]))
else:
s.add(If(picks[i-1] == -3, picks[i] == -3, pickReward(picks[i])))
while s.check() == sat:
m = s.model()
picked = []
for i in picks:
picked += [m[i]]
print(picked)
s.add(Or([p != v for p, v in zip(picks, picked)]))
When run, this prints:
[5, -3, -3, -3, -3, -3]
[1, 5, 5, 5, 5, 1]
[1, 2, 5, 5, 5, 1]
[2, 2, 5, 5, 5, 1]
[2, 5, 5, 5, 5, 1]
[2, 1, 5, 5, 5, 1]
[1, 1, 5, 5, 5, 1]
[2, 1, 5, 5, 5, 2]
[2, 5, 5, 5, 5, 2]
[2, 5, 5, 5, 5, 5]
[2, 5, 5, 5, 5, -3]
[2, 1, 5, 5, 5, 5]
...
I interrupted the above as it keeps enumerating all the possible picks. There are a total of 1093 of them in this particular case.
(You can get different answers depending on your version of z3.)
Hope this gets you started. Stating what your original goal is directly is usually much more helpful, should you have further questions.

How to define the nodes and elements in ABAQUS input file?

I have edited an ABAQUS input file generated from the maple code written to create a 3D model for analysis. The input file is missing the nodes and element definitions. Can someone give me some example on how to edit the following code to input the model for analysis.
*HEADING
Woven Unit Cell
***NODE DEFINITION***
*NODE, NSET=NALL
***ELEMENT DEFINITION***
***MATRIX***
*ELEMENT, TYPE=C3D8, ELSET=MATRIX
***FIBERS***
*ELEMENT, TYPE=C3D8, ELSET=FIBER1
*ELEMENT, TYPE=C3D8, ELSET=FIBER2
*ELEMENT, TYPE=C3D8, ELSET=FIBER3
*ELEMENT, TYPE=C3D8, ELSET=FIBER4
*MATERIAL, NAME=MMATRIX
*ELASTIC, TYPE=ISOTROPIC
3.45E+9, 0.35
*MATERIAL, NAME=MFIBER1
*ELASTIC, TYPE=ENGINEERING CONSTANTS
1.72E+11, 4.72E+09, 4.72E+09, 0.4, 0.4, 0.4, 3.6E+10, 3.6E+10
3.6E+10
*MATERIAL, NAME=MFIBER2
*ELASTIC, TYPE=ENGINEERING CONSTANTS
1.72E+11, 4.72E+09, 4.72E+09, 0.4, 0.4, 0.4, 3.6E+10, 3.6E+10
3.6E+10
*MATERIAL, NAME=MFIBER3
*ELASTIC, TYPE=ENGINEERING CONSTANTS
1.72E+11, 4.72E+09, 4.72E+09, 0.4, 0.4, 0.4, 3.6E+10, 3.6E+10
3.6E+10
*MATERIAL, NAME=MFIBER4
*ELASTIC, TYPE=ENGINEERING CONSTANTS
1.72E+11, 4.72E+09, 4.72E+09, 0.4, 0.4, 0.4, 3.6E+10, 3.6E+10
3.6E+10
*SOLID SECTION, ELSET=MATRIX, MATERIAL=MMATRIX
*SOLID SECTION, ELSET=FIBER1, MATERIAL=MFIBER1, ORIENTATION=FTRANS
*SOLID SECTION, ELSET=FIBER2, MATERIAL=MFIBER2, ORIENTATION=FTRANS
*SOLID SECTION, ELSET=FIBER3, MATERIAL=MFIBER3, ORIENTATION=FLONG
*SOLID SECTION, ELSET=FIBER4, MATERIAL=MFIBER4, ORIENTATION=FLONG
*ORIENTATION, NAME=FLONG, SYSTEM=RECTANGULAR, DEFINITION=COORDINATES
1., 0., 0., 0., 1., 0.
*ORIENTATION, NAME=FTRANS, SYSTEM=RECTANGULAR, DEFINITION=COORDINATES
0., 1., 0., -1., 0., 0.
***NODE SETS***
*NSET, NSET=BASE
*NSET, NSET=TOP
*NSET, NSET=XMIN
*NSET, NSET=XMAX
*NSET, NSET=YMIN
*NSET, NSET=YMAX
*STEP, NLGEOM
*STATIC
*RESTART, WRITE, FREQ=1
***BOUNDARY CONDITIONS***
*BOUNDARY
BASE,3,,0.
TOP,3,,0.
XMIN,1,,0.
XMAX,1,,0.001
YMIN,2,,0.
YMAX,2,,0.
***NODE SET DEFINITION***
*ELSET, ELSET=ALLF
FIBER1,FIBER2,FIBER3,FIBER4
*END STEP
An ID followed by the coordinates identifies each node.
For one C3D8 element you need at least 8 nodes. For instance:
*NODE, NSET=NALL
1, 1., 1., 1.
2, 1., 0., 1.
3, 1., 1., 0.
4, 1., 0., 0.
5, 0., 1., 1.
6, 0., 0., 1.
7, 0., 1., 0.
8, 0., 0., 0.
Similarly, each element has an ID and the nodes in the right order.
Each C3D8 element can be created as, for instance:
*ELEMENT, TYPE=C3D8, ELSET=MATRIX
1, 5, 6, 8, 7, 1, 2, 4, 3
This scheme may help you.
** 8-node 7-----------3
** /| /|
** / | / | y
** 5-----------1 | |
** | | | | |
** | | | | |
** | 8--------|--4 0-----> x
** | / | / /
** |/ |/ /
** 6-----------2 z origin at node 8

Abaqus - How to apply initial conditions as plastic strain?

I m trying to apply to apply plastic strains initial conditions in Abaqus as you can see below : 
** ----------------------------------------------------------------
*Initial Conditions, type=PLASTIC STRAIN
test_elements, 0.338, -0.276, -0.0618, 0.0447
**
** STEP: Step-1
This part of code works in case of Stresses but not for plastic strains... The calculation presents no errors, abaqus runs but when i check the results, it doesnt consider my plastic strain...
----EDIT LATER---
An example of INP file :
*Heading
** Job name: dernier1 Model name: test_plastic
** Generated by: Abaqus/CAE 6.13-1
*Preprint, echo=NO, model=NO, history=NO, contact=NO
**
** PARTS
**
*Part, name=PART-1-1
*Node
1, 0., -13.75, 3.
2, -15., -13.75, 3.
3, -30., -13.75, 3.
4, 0., -13.75, 0.
5, -15., -13.75, 0.
6, -30., -13.75, 0.
7, 0., 2.5, 3.
8, -15.3125, 2.5, 3.
9, -30.625, 2.5, 3.
10, 0., 2.5, 0.
11, -15.3125, 2.5, 0.
12, -30.625, 2.5, 0.
13, 0., 18.75, 3.
14, -15.625, 18.75, 3.
15, -31.25, 18.75, 3.
16, 0., 18.75, 0.
17, -15.625, 18.75, 0.
18, -31.25, 18.75, 0.
*Element, type=C3D8R
1, 7, 8, 11, 10, 1, 2, 5, 4
2, 8, 9, 12, 11, 2, 3, 6, 5
3, 13, 14, 17, 16, 7, 8, 11, 10
4, 14, 15, 18, 17, 8, 9, 12, 11
*Nset, nset=SET-1, generate
1, 18, 1
*Elset, elset=SET-1, generate
1, 4, 1
** Section: Section-1-SET-1
*Solid Section, elset=SET-1, material=MATERIAL-1
,
*End Part
**
**
** ASSEMBLY
**
*Assembly, name=Assembly
**
*Instance, name=PART-1-1, part=PART-1-1
*End Instance
**
*Nset, nset=SET-1, instance=PART-1-1, generate
1, 6, 1
*Elset, elset=SET-1, instance=PART-1-1
1, 2
*Elset, elset="test_elements", instance=PART-1-1
1, 2
*Elset, elset=_SURF-1_S1, internal, instance=PART-1-1
3, 4
*Elset, elset=_SURF-1_S1_1, internal, instance=PART-1-1
3, 4
*Surface, type=ELEMENT, name=SURF-1
_SURF-1_S1_1, S1
*End Assembly
*Amplitude, name=AMP-1, time=TOTAL TIME, definition=PERIODIC
1, 5., 0., 0.
5., 1.
**
** MATERIALS
**
*Material, name=MATERIAL-1
*Elastic
140000., 0.28
*Plastic
50., 0.
100., 0.2
** ----------------------------------------------------------------
*Initial Conditions, type=PLASTIC STRAIN
test_elements, 0.338, 0., 0., 0.
**
** STEP: Step-1
**
*Step, name=Step-1, nlgeom=NO
*Static
5., 5., 5e-05, 5.
**
** BOUNDARY CONDITIONS
**
** Name: Disp-BC-1 Type: Symmetry/Antisymmetry/Encastre
*Boundary
SET-1, ENCASTRE
**
** LOADS
**
** Name: SURFFORCE-1 Type: Pressure
*Dsload, amplitude=AMP-1
SURF-1, P, -5
**
** OUTPUT REQUESTS
**
*Restart, write, frequency=0
**
** FIELD OUTPUT: F-Output-1
**
*Output, field, variable=PRESELECT
**
** HISTORY OUTPUT: H-Output-1
**
*Output, history, variable=PRESELECT
*End Step
So the only problem to get this to run was the extra set of quotes on test_elements? Not sure what the remaining problem is?
*Heading
** Job name: dernier1 Model name: test_plastic
** Generated by: Abaqus/CAE 6.13-1
*Preprint, echo=NO, model=NO, history=NO, contact=NO
**
**
*Node
1, 0., -13.75, 3.
2, -15., -13.75, 3.
3, -30., -13.75, 3.
4, 0., -13.75, 0.
5, -15., -13.75, 0.
6, -30., -13.75, 0.
7, 0., 2.5, 3.
8, -15.3125, 2.5, 3.
9, -30.625, 2.5, 3.
10, 0., 2.5, 0.
11, -15.3125, 2.5, 0.
12, -30.625, 2.5, 0.
13, 0., 18.75, 3.
14, -15.625, 18.75, 3.
15, -31.25, 18.75, 3.
16, 0., 18.75, 0.
17, -15.625, 18.75, 0.
18, -31.25, 18.75, 0.
*Element, type=C3D8R
1, 7, 8, 11, 10, 1, 2, 5, 4
2, 8, 9, 12, 11, 2, 3, 6, 5
3, 13, 14, 17, 16, 7, 8, 11, 10
4, 14, 15, 18, 17, 8, 9, 12, 11
*Nset, nset=SET-1, generate
1, 18, 1
*Elset, elset=SET-1, generate
1, 4, 1
** Section: Section-1-SET-1
*Solid Section, elset=SET-1, material=MATERIAL-1
,
**
*Nset, nset=SET-1, generate
1, 6, 1
*Elset, elset=SET-1
1, 2
*Elset, elset=test_elements
1, 2
*Elset, elset=_SURF-1_S1
3, 4
*Elset, elset=_SURF-1_S1_1
3, 4
*Surface, type=ELEMENT, name=SURF-1
_SURF-1_S1_1, S1
*End Assembly
*Amplitude, name=AMP-1, time=TOTAL TIME, definition=PERIODIC
1, 5., 0., 0.
5., 1.
**
** MATERIALS
**
*Material, name=MATERIAL-1
*Elastic
140000., 0.28
*Plastic
50., 0.
100., 0.2
** ----------------------------------------------------------------
*Initial Conditions, type=PLASTIC STRAIN
test_elements, 0.338, 0., 0., 0.
**
** STEP: Step-1
**
*Step, name=Step-1, nlgeom=NO
*Static
5., 5., 5e-05, 5.
**
** BOUNDARY CONDITIONS
**
** Name: Disp-BC-1 Type: Symmetry/Antisymmetry/Encastre
*Boundary
SET-1, ENCASTRE
**
** LOADS
**
** Name: SURFFORCE-1 Type: Pressure
*Dsload, amplitude=AMP-1
SURF-1, P, -5
**
** OUTPUT REQUESTS
**
*Restart, write, frequency=0
**
** FIELD OUTPUT: F-Output-1
**
*Output, field, variable=PRESELECT
**
** HISTORY OUTPUT: H-Output-1
**
*Output, history, variable=PRESELECT
*End Step

ruby/rails array all elements between two indices

I have an array like this: [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
What's the simplest way to return each item in the array from position 6 until 0 where the resulting array looks like: [1,2,3,4,5,6,7]
This positions in the array can be dynamic, for example passing in 4 and 9 should return [11,12,1,2,3,4]
I'm wondering if there's a method that accomplishes this in Rails api.
Thanks in advance
EDIT
Let's assume that no negative numbers, so doing array[2..-2] wont work.
Array#splice almost works for this, but if the second position is less than the first, it returns nil.
def foo a, min, max
a.rotate(min).first((max - min) % a.length + 1)
end
a = [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
foo(a, 6, 0) # => [1, 2, 3, 4, 5, 6, 7]
foo(a, 4, 9) # => [11, 12, 1, 2, 3, 4]
class Array
def get_sub_array(start,last)
(start > last) ? (self[start..-1] + self[0..last]) : self[start..last]
end
end
Then
a = [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
a.get_sub_array(6,0)
#[1, 2, 3, 4, 5, 6, 7]
Or if you don't want to monkey patch
You could have a method like
def get_sub_array(array, start,last)
(start > last) ? (array[start..-1] + array[0..last]) : array[start..last]
end
a = [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
get_sub_array(a,6,0)
#[1, 2, 3, 4, 5, 6, 7]
def some_function(some_array,start_val=6, end_val=0)
if end_val > start_val
some_array[start_val,(end_val - start_val)]
else
(some_array[start_val, some_array.size] << some_array[0, (end_val)]).flatten
end
end
You can use ternary operator to make it one liner too:
def some_function(some_array,start_val=6, end_val=0)
end_val > start_val ? some_array[start_val,(end_val - start_val)] : (some_array[start_val, some_array.size] << some_array[0, (end_val)]).flatten
end
a = [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
some_function(a) # => [1, 2, 3, 4, 5, 6, 7]
some_function(a, 4, 9) # => [11, 12, 1, 2, 3, 4]
min=6
max=0
arr = [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
result = []
if max<min
result << arr[min..arr.length]
result << arr[0..max]
else
result << arr[min..max]
end
A couple more ways (my preference being for #1).
a = [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
#1
def foo a, min, max
as = a.size
max += as if max < min
(min..max).map { |i| a[i%as] }
end
foo(a, 6, 0) # => [ 1, 2, 3, 4, 5, 6, 7]
foo(a, 4, 9) # => [11, 12, 1, 2, 3, 4]
#2
def foo a, min, max
max += a.size if max < min
e = a.cycle
min.times { e.next }
(max-min+1).times.map { e.next }
end
foo(a, 6, 0) # => [ 1, 2, 3, 4, 5, 6, 7]
foo(a, 4, 9) # => [11, 12, 1, 2, 3, 4]
def foo a, s, e
a = e < s ? (a[s,a.size] << a[0..e]).flatten : a[s..e]
end
a = [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
a = foo(a, 6, 0) # => [1, 2, 3, 4, 5, 6, 7]
a = foo(a, 4, 9) # => [11, 12, 1, 2, 3, 4]
myArray = [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
myArray[6..-1] returns [1, 2, 3, 4, 5, 6]
myArray[4..9] returns [11,12,1,2,3,4]

Resources